]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/telnet/telnetd/sys_term.c
Upgrade to OpenSSH 7.9p1.
[FreeBSD/FreeBSD.git] / contrib / telnet / telnetd / sys_term.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #if 0
31 #ifndef lint
32 static const char sccsid[] = "@(#)sys_term.c    8.4+1 (Berkeley) 5/30/95";
33 #endif
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/types.h>
39 #include <sys/tty.h>
40 #include <libutil.h>
41 #include <stdlib.h>
42
43 #include "telnetd.h"
44 #include "pathnames.h"
45 #include "types.h"
46 #include "baud.h"
47
48 #ifdef  AUTHENTICATION
49 #include <libtelnet/auth.h>
50 #endif
51
52 int cleanopen(char *);
53 void scrub_env(void);
54
55 char    *envinit[3];
56 extern char **environ;
57
58 #define SCPYN(a, b)     (void) strncpy(a, b, sizeof(a))
59 #define SCMPN(a, b)     strncmp(a, b, sizeof(a))
60
61 #ifdef  t_erase
62 #undef  t_erase
63 #undef  t_kill
64 #undef  t_intrc
65 #undef  t_quitc
66 #undef  t_startc
67 #undef  t_stopc
68 #undef  t_eofc
69 #undef  t_brkc
70 #undef  t_suspc
71 #undef  t_dsuspc
72 #undef  t_rprntc
73 #undef  t_flushc
74 #undef  t_werasc
75 #undef  t_lnextc
76 #endif
77
78 #ifndef USE_TERMIO
79 struct termbuf {
80         struct sgttyb sg;
81         struct tchars tc;
82         struct ltchars ltc;
83         int state;
84         int lflags;
85 } termbuf, termbuf2;
86 # define        cfsetospeed(tp, val)    (tp)->sg.sg_ospeed = (val)
87 # define        cfsetispeed(tp, val)    (tp)->sg.sg_ispeed = (val)
88 # define        cfgetospeed(tp)         (tp)->sg.sg_ospeed
89 # define        cfgetispeed(tp)         (tp)->sg.sg_ispeed
90 #else   /* USE_TERMIO */
91 # ifndef        TCSANOW
92 #  ifdef TCSETS
93 #   define      TCSANOW         TCSETS
94 #   define      TCSADRAIN       TCSETSW
95 #   define      tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
96 #  else
97 #   ifdef TCSETA
98 #    define     TCSANOW         TCSETA
99 #    define     TCSADRAIN       TCSETAW
100 #    define     tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
101 #   else
102 #    define     TCSANOW         TIOCSETA
103 #    define     TCSADRAIN       TIOCSETAW
104 #    define     tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
105 #   endif
106 #  endif
107 #  define       tcsetattr(f, a, t)      ioctl(f, a, t)
108 #  define       cfsetospeed(tp, val)    (tp)->c_cflag &= ~CBAUD; \
109                                         (tp)->c_cflag |= (val)
110 #  define       cfgetospeed(tp)         ((tp)->c_cflag & CBAUD)
111 #  ifdef CIBAUD
112 #   define      cfsetispeed(tp, val)    (tp)->c_cflag &= ~CIBAUD; \
113                                         (tp)->c_cflag |= ((val)<<IBSHIFT)
114 #   define      cfgetispeed(tp)         (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
115 #  else
116 #   define      cfsetispeed(tp, val)    (tp)->c_cflag &= ~CBAUD; \
117                                         (tp)->c_cflag |= (val)
118 #   define      cfgetispeed(tp)         ((tp)->c_cflag & CBAUD)
119 #  endif
120 # endif /* TCSANOW */
121 struct termios termbuf, termbuf2;       /* pty control structure */
122 #endif  /* USE_TERMIO */
123
124 #include <sys/types.h>
125 #include <libutil.h>
126
127 int cleanopen(char *);
128 void scrub_env(void);
129 static char **addarg(char **, const char *);
130
131 /*
132  * init_termbuf()
133  * copy_termbuf(cp)
134  * set_termbuf()
135  *
136  * These three routines are used to get and set the "termbuf" structure
137  * to and from the kernel.  init_termbuf() gets the current settings.
138  * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
139  * set_termbuf() writes the structure into the kernel.
140  */
141
142 void
143 init_termbuf(void)
144 {
145 #ifndef USE_TERMIO
146         (void) ioctl(pty, TIOCGETP, (char *)&termbuf.sg);
147         (void) ioctl(pty, TIOCGETC, (char *)&termbuf.tc);
148         (void) ioctl(pty, TIOCGLTC, (char *)&termbuf.ltc);
149 # ifdef TIOCGSTATE
150         (void) ioctl(pty, TIOCGSTATE, (char *)&termbuf.state);
151 # endif
152 #else
153         (void) tcgetattr(pty, &termbuf);
154 #endif
155         termbuf2 = termbuf;
156 }
157
158 #if     defined(LINEMODE) && defined(TIOCPKT_IOCTL)
159 void
160 copy_termbuf(char *cp, size_t len)
161 {
162         if (len > sizeof(termbuf))
163                 len = sizeof(termbuf);
164         memmove((char *)&termbuf, cp, len);
165         termbuf2 = termbuf;
166 }
167 #endif  /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
168
169 void
170 set_termbuf(void)
171 {
172         /*
173          * Only make the necessary changes.
174          */
175 #ifndef USE_TERMIO
176         if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg,
177                                                         sizeof(termbuf.sg)))
178                 (void) ioctl(pty, TIOCSETN, (char *)&termbuf.sg);
179         if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc,
180                                                         sizeof(termbuf.tc)))
181                 (void) ioctl(pty, TIOCSETC, (char *)&termbuf.tc);
182         if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc,
183                                                         sizeof(termbuf.ltc)))
184                 (void) ioctl(pty, TIOCSLTC, (char *)&termbuf.ltc);
185         if (termbuf.lflags != termbuf2.lflags)
186                 (void) ioctl(pty, TIOCLSET, (char *)&termbuf.lflags);
187 #else   /* USE_TERMIO */
188         if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
189                 (void) tcsetattr(pty, TCSANOW, &termbuf);
190 #endif  /* USE_TERMIO */
191 }
192
193
194 /*
195  * spcset(func, valp, valpp)
196  *
197  * This function takes various special characters (func), and
198  * sets *valp to the current value of that character, and
199  * *valpp to point to where in the "termbuf" structure that
200  * value is kept.
201  *
202  * It returns the SLC_ level of support for this function.
203  */
204
205 #ifndef USE_TERMIO
206 int
207 spcset(int func, cc_t *valp, cc_t **valpp)
208 {
209         switch(func) {
210         case SLC_EOF:
211                 *valp = termbuf.tc.t_eofc;
212                 *valpp = (cc_t *)&termbuf.tc.t_eofc;
213                 return(SLC_VARIABLE);
214         case SLC_EC:
215                 *valp = termbuf.sg.sg_erase;
216                 *valpp = (cc_t *)&termbuf.sg.sg_erase;
217                 return(SLC_VARIABLE);
218         case SLC_EL:
219                 *valp = termbuf.sg.sg_kill;
220                 *valpp = (cc_t *)&termbuf.sg.sg_kill;
221                 return(SLC_VARIABLE);
222         case SLC_IP:
223                 *valp = termbuf.tc.t_intrc;
224                 *valpp = (cc_t *)&termbuf.tc.t_intrc;
225                 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
226         case SLC_ABORT:
227                 *valp = termbuf.tc.t_quitc;
228                 *valpp = (cc_t *)&termbuf.tc.t_quitc;
229                 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
230         case SLC_XON:
231                 *valp = termbuf.tc.t_startc;
232                 *valpp = (cc_t *)&termbuf.tc.t_startc;
233                 return(SLC_VARIABLE);
234         case SLC_XOFF:
235                 *valp = termbuf.tc.t_stopc;
236                 *valpp = (cc_t *)&termbuf.tc.t_stopc;
237                 return(SLC_VARIABLE);
238         case SLC_AO:
239                 *valp = termbuf.ltc.t_flushc;
240                 *valpp = (cc_t *)&termbuf.ltc.t_flushc;
241                 return(SLC_VARIABLE);
242         case SLC_SUSP:
243                 *valp = termbuf.ltc.t_suspc;
244                 *valpp = (cc_t *)&termbuf.ltc.t_suspc;
245                 return(SLC_VARIABLE);
246         case SLC_EW:
247                 *valp = termbuf.ltc.t_werasc;
248                 *valpp = (cc_t *)&termbuf.ltc.t_werasc;
249                 return(SLC_VARIABLE);
250         case SLC_RP:
251                 *valp = termbuf.ltc.t_rprntc;
252                 *valpp = (cc_t *)&termbuf.ltc.t_rprntc;
253                 return(SLC_VARIABLE);
254         case SLC_LNEXT:
255                 *valp = termbuf.ltc.t_lnextc;
256                 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
257                 return(SLC_VARIABLE);
258         case SLC_FORW1:
259                 *valp = termbuf.tc.t_brkc;
260                 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
261                 return(SLC_VARIABLE);
262         case SLC_BRK:
263         case SLC_SYNCH:
264         case SLC_AYT:
265         case SLC_EOR:
266                 *valp = (cc_t)0;
267                 *valpp = (cc_t *)0;
268                 return(SLC_DEFAULT);
269         default:
270                 *valp = (cc_t)0;
271                 *valpp = (cc_t *)0;
272                 return(SLC_NOSUPPORT);
273         }
274 }
275
276 #else   /* USE_TERMIO */
277
278
279 #define setval(a, b)    *valp = termbuf.c_cc[a]; \
280                         *valpp = &termbuf.c_cc[a]; \
281                         return(b);
282 #define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT);
283
284 int
285 spcset(int func, cc_t *valp, cc_t **valpp)
286 {
287         switch(func) {
288         case SLC_EOF:
289                 setval(VEOF, SLC_VARIABLE);
290         case SLC_EC:
291                 setval(VERASE, SLC_VARIABLE);
292         case SLC_EL:
293                 setval(VKILL, SLC_VARIABLE);
294         case SLC_IP:
295                 setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
296         case SLC_ABORT:
297                 setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
298         case SLC_XON:
299 #ifdef  VSTART
300                 setval(VSTART, SLC_VARIABLE);
301 #else
302                 defval(0x13);
303 #endif
304         case SLC_XOFF:
305 #ifdef  VSTOP
306                 setval(VSTOP, SLC_VARIABLE);
307 #else
308                 defval(0x11);
309 #endif
310         case SLC_EW:
311 #ifdef  VWERASE
312                 setval(VWERASE, SLC_VARIABLE);
313 #else
314                 defval(0);
315 #endif
316         case SLC_RP:
317 #ifdef  VREPRINT
318                 setval(VREPRINT, SLC_VARIABLE);
319 #else
320                 defval(0);
321 #endif
322         case SLC_LNEXT:
323 #ifdef  VLNEXT
324                 setval(VLNEXT, SLC_VARIABLE);
325 #else
326                 defval(0);
327 #endif
328         case SLC_AO:
329 #if     !defined(VDISCARD) && defined(VFLUSHO)
330 # define VDISCARD VFLUSHO
331 #endif
332 #ifdef  VDISCARD
333                 setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
334 #else
335                 defval(0);
336 #endif
337         case SLC_SUSP:
338 #ifdef  VSUSP
339                 setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
340 #else
341                 defval(0);
342 #endif
343 #ifdef  VEOL
344         case SLC_FORW1:
345                 setval(VEOL, SLC_VARIABLE);
346 #endif
347 #ifdef  VEOL2
348         case SLC_FORW2:
349                 setval(VEOL2, SLC_VARIABLE);
350 #endif
351         case SLC_AYT:
352 #ifdef  VSTATUS
353                 setval(VSTATUS, SLC_VARIABLE);
354 #else
355                 defval(0);
356 #endif
357
358         case SLC_BRK:
359         case SLC_SYNCH:
360         case SLC_EOR:
361                 defval(0);
362
363         default:
364                 *valp = 0;
365                 *valpp = 0;
366                 return(SLC_NOSUPPORT);
367         }
368 }
369 #endif  /* USE_TERMIO */
370
371 /*
372  * getpty()
373  *
374  * Allocate a pty.  As a side effect, the external character
375  * array "line" contains the name of the slave side.
376  *
377  * Returns the file descriptor of the opened pty.
378  */
379 char line[32];
380
381 int
382 getpty(int *ptynum __unused)
383 {
384         int p;
385         const char *pn;
386
387         p = posix_openpt(O_RDWR|O_NOCTTY);
388         if (p < 0)
389                 return (-1);
390         
391         if (grantpt(p) == -1)
392                 return (-1);
393
394         if (unlockpt(p) == -1)
395                 return (-1);
396         
397         pn = ptsname(p);
398         if (pn == NULL)
399                 return (-1);
400         
401         if (strlcpy(line, pn, sizeof line) >= sizeof line)
402                 return (-1);
403
404         return (p);
405 }
406
407 #ifdef  LINEMODE
408 /*
409  * tty_flowmode()       Find out if flow control is enabled or disabled.
410  * tty_linemode()       Find out if linemode (external processing) is enabled.
411  * tty_setlinemod(on)   Turn on/off linemode.
412  * tty_isecho()         Find out if echoing is turned on.
413  * tty_setecho(on)      Enable/disable character echoing.
414  * tty_israw()          Find out if terminal is in RAW mode.
415  * tty_binaryin(on)     Turn on/off BINARY on input.
416  * tty_binaryout(on)    Turn on/off BINARY on output.
417  * tty_isediting()      Find out if line editing is enabled.
418  * tty_istrapsig()      Find out if signal trapping is enabled.
419  * tty_setedit(on)      Turn on/off line editing.
420  * tty_setsig(on)       Turn on/off signal trapping.
421  * tty_issofttab()      Find out if tab expansion is enabled.
422  * tty_setsofttab(on)   Turn on/off soft tab expansion.
423  * tty_islitecho()      Find out if typed control chars are echoed literally
424  * tty_setlitecho()     Turn on/off literal echo of control chars
425  * tty_tspeed(val)      Set transmit speed to val.
426  * tty_rspeed(val)      Set receive speed to val.
427  */
428
429
430 int
431 tty_linemode(void)
432 {
433 #ifndef USE_TERMIO
434         return(termbuf.state & TS_EXTPROC);
435 #else
436         return(termbuf.c_lflag & EXTPROC);
437 #endif
438 }
439
440 void
441 tty_setlinemode(int on)
442 {
443 #ifdef  TIOCEXT
444         set_termbuf();
445         (void) ioctl(pty, TIOCEXT, (char *)&on);
446         init_termbuf();
447 #else   /* !TIOCEXT */
448 # ifdef EXTPROC
449         if (on)
450                 termbuf.c_lflag |= EXTPROC;
451         else
452                 termbuf.c_lflag &= ~EXTPROC;
453 # endif
454 #endif  /* TIOCEXT */
455 }
456 #endif  /* LINEMODE */
457
458 int
459 tty_isecho(void)
460 {
461 #ifndef USE_TERMIO
462         return (termbuf.sg.sg_flags & ECHO);
463 #else
464         return (termbuf.c_lflag & ECHO);
465 #endif
466 }
467
468 int
469 tty_flowmode(void)
470 {
471 #ifndef USE_TERMIO
472         return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0);
473 #else
474         return((termbuf.c_iflag & IXON) ? 1 : 0);
475 #endif
476 }
477
478 int
479 tty_restartany(void)
480 {
481 #ifndef USE_TERMIO
482 # ifdef DECCTQ
483         return((termbuf.lflags & DECCTQ) ? 0 : 1);
484 # else
485         return(-1);
486 # endif
487 #else
488         return((termbuf.c_iflag & IXANY) ? 1 : 0);
489 #endif
490 }
491
492 void
493 tty_setecho(int on)
494 {
495 #ifndef USE_TERMIO
496         if (on)
497                 termbuf.sg.sg_flags |= ECHO|CRMOD;
498         else
499                 termbuf.sg.sg_flags &= ~(ECHO|CRMOD);
500 #else
501         if (on)
502                 termbuf.c_lflag |= ECHO;
503         else
504                 termbuf.c_lflag &= ~ECHO;
505 #endif
506 }
507
508 int
509 tty_israw(void)
510 {
511 #ifndef USE_TERMIO
512         return(termbuf.sg.sg_flags & RAW);
513 #else
514         return(!(termbuf.c_lflag & ICANON));
515 #endif
516 }
517
518 #ifdef  AUTHENTICATION
519 #if     defined(NO_LOGIN_F) && defined(LOGIN_R)
520 int
521 tty_setraw(int on)
522 {
523 #  ifndef USE_TERMIO
524         if (on)
525                 termbuf.sg.sg_flags |= RAW;
526         else
527                 termbuf.sg.sg_flags &= ~RAW;
528 #  else
529         if (on)
530                 termbuf.c_lflag &= ~ICANON;
531         else
532                 termbuf.c_lflag |= ICANON;
533 #  endif
534 }
535 #endif
536 #endif /* AUTHENTICATION */
537
538 void
539 tty_binaryin(int on)
540 {
541 #ifndef USE_TERMIO
542         if (on)
543                 termbuf.lflags |= LPASS8;
544         else
545                 termbuf.lflags &= ~LPASS8;
546 #else
547         if (on) {
548                 termbuf.c_iflag &= ~ISTRIP;
549         } else {
550                 termbuf.c_iflag |= ISTRIP;
551         }
552 #endif
553 }
554
555 void
556 tty_binaryout(int on)
557 {
558 #ifndef USE_TERMIO
559         if (on)
560                 termbuf.lflags |= LLITOUT;
561         else
562                 termbuf.lflags &= ~LLITOUT;
563 #else
564         if (on) {
565                 termbuf.c_cflag &= ~(CSIZE|PARENB);
566                 termbuf.c_cflag |= CS8;
567                 termbuf.c_oflag &= ~OPOST;
568         } else {
569                 termbuf.c_cflag &= ~CSIZE;
570                 termbuf.c_cflag |= CS7|PARENB;
571                 termbuf.c_oflag |= OPOST;
572         }
573 #endif
574 }
575
576 int
577 tty_isbinaryin(void)
578 {
579 #ifndef USE_TERMIO
580         return(termbuf.lflags & LPASS8);
581 #else
582         return(!(termbuf.c_iflag & ISTRIP));
583 #endif
584 }
585
586 int
587 tty_isbinaryout(void)
588 {
589 #ifndef USE_TERMIO
590         return(termbuf.lflags & LLITOUT);
591 #else
592         return(!(termbuf.c_oflag&OPOST));
593 #endif
594 }
595
596 #ifdef  LINEMODE
597 int
598 tty_isediting(void)
599 {
600 #ifndef USE_TERMIO
601         return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));
602 #else
603         return(termbuf.c_lflag & ICANON);
604 #endif
605 }
606
607 int
608 tty_istrapsig(void)
609 {
610 #ifndef USE_TERMIO
611         return(!(termbuf.sg.sg_flags&RAW));
612 #else
613         return(termbuf.c_lflag & ISIG);
614 #endif
615 }
616
617 void
618 tty_setedit(int on)
619 {
620 #ifndef USE_TERMIO
621         if (on)
622                 termbuf.sg.sg_flags &= ~CBREAK;
623         else
624                 termbuf.sg.sg_flags |= CBREAK;
625 #else
626         if (on)
627                 termbuf.c_lflag |= ICANON;
628         else
629                 termbuf.c_lflag &= ~ICANON;
630 #endif
631 }
632
633 void
634 tty_setsig(int on)
635 {
636 #ifndef USE_TERMIO
637         if (on)
638                 ;
639 #else
640         if (on)
641                 termbuf.c_lflag |= ISIG;
642         else
643                 termbuf.c_lflag &= ~ISIG;
644 #endif
645 }
646 #endif  /* LINEMODE */
647
648 int
649 tty_issofttab(void)
650 {
651 #ifndef USE_TERMIO
652         return (termbuf.sg.sg_flags & XTABS);
653 #else
654 # ifdef OXTABS
655         return (termbuf.c_oflag & OXTABS);
656 # endif
657 # ifdef TABDLY
658         return ((termbuf.c_oflag & TABDLY) == TAB3);
659 # endif
660 #endif
661 }
662
663 void
664 tty_setsofttab(int on)
665 {
666 #ifndef USE_TERMIO
667         if (on)
668                 termbuf.sg.sg_flags |= XTABS;
669         else
670                 termbuf.sg.sg_flags &= ~XTABS;
671 #else
672         if (on) {
673 # ifdef OXTABS
674                 termbuf.c_oflag |= OXTABS;
675 # endif
676 # ifdef TABDLY
677                 termbuf.c_oflag &= ~TABDLY;
678                 termbuf.c_oflag |= TAB3;
679 # endif
680         } else {
681 # ifdef OXTABS
682                 termbuf.c_oflag &= ~OXTABS;
683 # endif
684 # ifdef TABDLY
685                 termbuf.c_oflag &= ~TABDLY;
686                 termbuf.c_oflag |= TAB0;
687 # endif
688         }
689 #endif
690 }
691
692 int
693 tty_islitecho(void)
694 {
695 #ifndef USE_TERMIO
696         return (!(termbuf.lflags & LCTLECH));
697 #else
698 # ifdef ECHOCTL
699         return (!(termbuf.c_lflag & ECHOCTL));
700 # endif
701 # ifdef TCTLECH
702         return (!(termbuf.c_lflag & TCTLECH));
703 # endif
704 # if    !defined(ECHOCTL) && !defined(TCTLECH)
705         return (0);     /* assumes ctl chars are echoed '^x' */
706 # endif
707 #endif
708 }
709
710 void
711 tty_setlitecho(int on)
712 {
713 #ifndef USE_TERMIO
714         if (on)
715                 termbuf.lflags &= ~LCTLECH;
716         else
717                 termbuf.lflags |= LCTLECH;
718 #else
719 # ifdef ECHOCTL
720         if (on)
721                 termbuf.c_lflag &= ~ECHOCTL;
722         else
723                 termbuf.c_lflag |= ECHOCTL;
724 # endif
725 # ifdef TCTLECH
726         if (on)
727                 termbuf.c_lflag &= ~TCTLECH;
728         else
729                 termbuf.c_lflag |= TCTLECH;
730 # endif
731 #endif
732 }
733
734 int
735 tty_iscrnl(void)
736 {
737 #ifndef USE_TERMIO
738         return (termbuf.sg.sg_flags & CRMOD);
739 #else
740         return (termbuf.c_iflag & ICRNL);
741 #endif
742 }
743
744 void
745 tty_tspeed(int val)
746 {
747 #ifdef  DECODE_BAUD
748         struct termspeeds *tp;
749
750         for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
751                 ;
752         if (tp->speed == -1)    /* back up to last valid value */
753                 --tp;
754         cfsetospeed(&termbuf, tp->value);
755 #else   /* DECODE_BAUD */
756         cfsetospeed(&termbuf, val);
757 #endif  /* DECODE_BAUD */
758 }
759
760 void
761 tty_rspeed(int val)
762 {
763 #ifdef  DECODE_BAUD
764         struct termspeeds *tp;
765
766         for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
767                 ;
768         if (tp->speed == -1)    /* back up to last valid value */
769                 --tp;
770         cfsetispeed(&termbuf, tp->value);
771 #else   /* DECODE_BAUD */
772         cfsetispeed(&termbuf, val);
773 #endif  /* DECODE_BAUD */
774 }
775
776 /*
777  * getptyslave()
778  *
779  * Open the slave side of the pty, and do any initialization
780  * that is necessary.
781  */
782 static void
783 getptyslave(void)
784 {
785         int t = -1;
786         char erase;
787
788 # ifdef LINEMODE
789         int waslm;
790 # endif
791 # ifdef TIOCGWINSZ
792         struct winsize ws;
793         extern int def_row, def_col;
794 # endif
795         extern int def_tspeed, def_rspeed;
796         /*
797          * Opening the slave side may cause initilization of the
798          * kernel tty structure.  We need remember the state of
799          *      if linemode was turned on
800          *      terminal window size
801          *      terminal speed
802          *      erase character
803          * so that we can re-set them if we need to.
804          */
805 # ifdef LINEMODE
806         waslm = tty_linemode();
807 # endif
808         erase = termbuf.c_cc[VERASE];
809
810         /*
811          * Make sure that we don't have a controlling tty, and
812          * that we are the session (process group) leader.
813          */
814 # ifdef TIOCNOTTY
815         t = open(_PATH_TTY, O_RDWR);
816         if (t >= 0) {
817                 (void) ioctl(t, TIOCNOTTY, (char *)0);
818                 (void) close(t);
819         }
820 # endif
821
822         t = cleanopen(line);
823         if (t < 0)
824                 fatalperror(net, line);
825
826
827         /*
828          * set up the tty modes as we like them to be.
829          */
830         init_termbuf();
831 # ifdef TIOCGWINSZ
832         if (def_row || def_col) {
833                 memset((char *)&ws, 0, sizeof(ws));
834                 ws.ws_col = def_col;
835                 ws.ws_row = def_row;
836                 (void)ioctl(t, TIOCSWINSZ, (char *)&ws);
837         }
838 # endif
839
840         /*
841          * Settings for sgtty based systems
842          */
843 # ifndef        USE_TERMIO
844         termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
845 # endif /* USE_TERMIO */
846
847         /*
848          * Settings for all other termios/termio based
849          * systems, other than 4.4BSD.  In 4.4BSD the
850          * kernel does the initial terminal setup.
851          */
852         tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
853         tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
854         if (erase)
855                 termbuf.c_cc[VERASE] = erase;
856 # ifdef LINEMODE
857         if (waslm)
858                 tty_setlinemode(1);
859 # endif /* LINEMODE */
860
861         /*
862          * Set the tty modes, and make this our controlling tty.
863          */
864         set_termbuf();
865         if (login_tty(t) == -1)
866                 fatalperror(net, "login_tty");
867         if (net > 2)
868                 (void) close(net);
869 #ifdef  AUTHENTICATION
870 #if     defined(NO_LOGIN_F) && defined(LOGIN_R)
871         /*
872          * Leave the pty open so that we can write out the rlogin
873          * protocol for /bin/login, if the authentication works.
874          */
875 #else
876         if (pty > 2) {
877                 (void) close(pty);
878                 pty = -1;
879         }
880 #endif
881 #endif /* AUTHENTICATION */
882 }
883
884 #ifndef O_NOCTTY
885 #define O_NOCTTY        0
886 #endif
887 /*
888  * Open the specified slave side of the pty,
889  * making sure that we have a clean tty.
890  */
891 int
892 cleanopen(char *li)
893 {
894         int t;
895
896         /*
897          * Make sure that other people can't open the
898          * slave side of the connection.
899          */
900         (void) chown(li, 0, 0);
901         (void) chmod(li, 0600);
902
903         (void) revoke(li);
904
905         t = open(line, O_RDWR|O_NOCTTY);
906
907         if (t < 0)
908                 return(-1);
909
910         return(t);
911 }
912
913 /*
914  * startslave(host)
915  *
916  * Given a hostname, do whatever
917  * is necessary to startup the login process on the slave side of the pty.
918  */
919
920 /* ARGSUSED */
921 void
922 startslave(char *host, int autologin, char *autoname)
923 {
924         int i;
925
926 #ifdef  AUTHENTICATION
927         if (!autoname || !autoname[0])
928                 autologin = 0;
929
930         if (autologin < auth_level) {
931                 fatal(net, "Authorization failed");
932                 exit(1);
933         }
934 #endif
935
936
937         if ((i = fork()) < 0)
938                 fatalperror(net, "fork");
939         if (i) {
940         } else {
941                 getptyslave();
942                 start_login(host, autologin, autoname);
943                 /*NOTREACHED*/
944         }
945 }
946
947 void
948 init_env(void)
949 {
950         char **envp;
951
952         envp = envinit;
953         if ((*envp = getenv("TZ")))
954                 *envp++ -= 3;
955         *envp = 0;
956         environ = envinit;
957 }
958
959
960 /*
961  * start_login(host)
962  *
963  * Assuming that we are now running as a child processes, this
964  * function will turn us into the login process.
965  */
966
967 #ifndef AUTHENTICATION
968 #define undef1 __unused
969 #else
970 #define undef1
971 #endif
972
973 void
974 start_login(char *host undef1, int autologin undef1, char *name undef1)
975 {
976         char **argv;
977         char *user;
978
979         user = getenv("USER");
980         user = (user != NULL) ? strdup(user) : NULL;
981
982         scrub_env();
983
984         /*
985          * -h : pass on name of host.
986          *              WARNING:  -h is accepted by login if and only if
987          *                      getuid() == 0.
988          * -p : don't clobber the environment (so terminal type stays set).
989          *
990          * -f : force this login, he has already been authenticated
991          */
992         argv = addarg(0, "login");
993
994 #if     !defined(NO_LOGIN_H)
995 #ifdef  AUTHENTICATION
996 # if    defined(NO_LOGIN_F) && defined(LOGIN_R)
997         /*
998          * Don't add the "-h host" option if we are going
999          * to be adding the "-r host" option down below...
1000          */
1001         if ((auth_level < 0) || (autologin != AUTH_VALID))
1002 # endif
1003 #endif /* AUTHENTICATION */
1004         {
1005                 argv = addarg(argv, "-h");
1006                 argv = addarg(argv, host);
1007         }
1008 #endif
1009 #if     !defined(NO_LOGIN_P)
1010         argv = addarg(argv, "-p");
1011 #endif
1012 #ifdef  LINEMODE
1013         /*
1014          * Set the environment variable "LINEMODE" to either
1015          * "real" or "kludge" if we are operating in either
1016          * real or kludge linemode.
1017          */
1018         if (lmodetype == REAL_LINEMODE)
1019                 setenv("LINEMODE", "real", 1);
1020 # ifdef KLUDGELINEMODE
1021         else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK)
1022                 setenv("LINEMODE", "kludge", 1);
1023 # endif
1024 #endif
1025 #ifdef  BFTPDAEMON
1026         /*
1027          * Are we working as the bftp daemon?  If so, then ask login
1028          * to start bftp instead of shell.
1029          */
1030         if (bftpd) {
1031                 argv = addarg(argv, "-e");
1032                 argv = addarg(argv, BFTPPATH);
1033         } else
1034 #endif
1035 #ifdef  AUTHENTICATION
1036         if (auth_level >= 0 && autologin == AUTH_VALID) {
1037 # if    !defined(NO_LOGIN_F)
1038                 argv = addarg(argv, "-f");
1039                 argv = addarg(argv, "--");
1040                 argv = addarg(argv, name);
1041 # else
1042 #  if defined(LOGIN_R)
1043                 /*
1044                  * We don't have support for "login -f", but we
1045                  * can fool /bin/login into thinking that we are
1046                  * rlogind, and allow us to log in without a
1047                  * password.  The rlogin protocol expects
1048                  *      local-user\0remote-user\0term/speed\0
1049                  */
1050
1051                 if (pty > 2) {
1052                         char *cp;
1053                         char speed[128];
1054                         int isecho, israw, xpty, len;
1055                         extern int def_rspeed;
1056 #  ifndef LOGIN_HOST
1057                         /*
1058                          * Tell login that we are coming from "localhost".
1059                          * If we passed in the real host name, then the
1060                          * user would have to allow .rhost access from
1061                          * every machine that they want authenticated
1062                          * access to work from, which sort of defeats
1063                          * the purpose of an authenticated login...
1064                          * So, we tell login that the session is coming
1065                          * from "localhost", and the user will only have
1066                          * to have "localhost" in their .rhost file.
1067                          */
1068 #                       define LOGIN_HOST "localhost"
1069 #  endif
1070                         argv = addarg(argv, "-r");
1071                         argv = addarg(argv, LOGIN_HOST);
1072
1073                         xpty = pty;
1074                         pty = 0;
1075                         init_termbuf();
1076                         isecho = tty_isecho();
1077                         israw = tty_israw();
1078                         if (isecho || !israw) {
1079                                 tty_setecho(0);         /* Turn off echo */
1080                                 tty_setraw(1);          /* Turn on raw */
1081                                 set_termbuf();
1082                         }
1083                         len = strlen(name)+1;
1084                         write(xpty, name, len);
1085                         write(xpty, name, len);
1086                         snprintf(speed, sizeof(speed),
1087                                 "%s/%d", (cp = getenv("TERM")) ? cp : "",
1088                                 (def_rspeed > 0) ? def_rspeed : 9600);
1089                         len = strlen(speed)+1;
1090                         write(xpty, speed, len);
1091
1092                         if (isecho || !israw) {
1093                                 init_termbuf();
1094                                 tty_setecho(isecho);
1095                                 tty_setraw(israw);
1096                                 set_termbuf();
1097                                 if (!israw) {
1098                                         /*
1099                                          * Write a newline to ensure
1100                                          * that login will be able to
1101                                          * read the line...
1102                                          */
1103                                         write(xpty, "\n", 1);
1104                                 }
1105                         }
1106                         pty = xpty;
1107                 }
1108 #  else
1109                 argv = addarg(argv, "--");
1110                 argv = addarg(argv, name);
1111 #  endif
1112 # endif
1113         } else
1114 #endif
1115         if (user != NULL) {
1116                 argv = addarg(argv, "--");
1117                 argv = addarg(argv, user);
1118 #if     defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1119                 {
1120                         char **cpp;
1121                         for (cpp = environ; *cpp; cpp++)
1122                                 argv = addarg(argv, *cpp);
1123                 }
1124 #endif
1125         }
1126 #ifdef  AUTHENTICATION
1127 #if     defined(NO_LOGIN_F) && defined(LOGIN_R)
1128         if (pty > 2)
1129                 close(pty);
1130 #endif
1131 #endif /* AUTHENTICATION */
1132         closelog();
1133
1134         if (user != NULL)
1135                 free(user);
1136
1137         if (altlogin == NULL) {
1138                 altlogin = _PATH_LOGIN;
1139         }
1140         execv(altlogin, argv);
1141
1142         syslog(LOG_ERR, "%s: %m", altlogin);
1143         fatalperror(net, altlogin);
1144         /*NOTREACHED*/
1145 }
1146
1147 static char **
1148 addarg(char **argv, const char *val)
1149 {
1150         char **cpp;
1151
1152         if (argv == NULL) {
1153                 /*
1154                  * 10 entries, a leading length, and a null
1155                  */
1156                 argv = (char **)malloc(sizeof(*argv) * 12);
1157                 if (argv == NULL)
1158                         fatal(net, "failure allocating argument space");
1159                 *argv++ = (char *)10;
1160                 *argv = (char *)0;
1161         }
1162         for (cpp = argv; *cpp; cpp++)
1163                 ;
1164         if (cpp == &argv[(long)argv[-1]]) {
1165                 --argv;
1166                 *argv = (char *)((long)(*argv) + 10);
1167                 argv = (char **)realloc(argv, sizeof(*argv)*((long)(*argv) + 2));
1168                 if (argv == NULL)
1169                         fatal(net, "failure allocating argument space");
1170                 argv++;
1171                 cpp = &argv[(long)argv[-1] - 10];
1172         }
1173         if ((*cpp++ = strdup(val)) == NULL)
1174                 fatal(net, "failure allocating argument space");
1175         *cpp = 0;
1176         return(argv);
1177 }
1178
1179 /*
1180  * scrub_env()
1181  *
1182  * We only accept the environment variables listed below.
1183  */
1184 void
1185 scrub_env(void)
1186 {
1187         static const char *rej[] = {
1188                 "TERMCAP=/",
1189                 NULL
1190         };
1191
1192         static const char *acc[] = {
1193                 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
1194                 "TERM=",
1195                 "EDITOR=",
1196                 "PAGER=",
1197                 "LOGNAME=",
1198                 "POSIXLY_CORRECT=",
1199                 "PRINTER=",
1200                 NULL
1201         };
1202
1203         char **cpp, **cpp2;
1204         const char **p;
1205         char ** new_environ;
1206         size_t count;
1207
1208         /* Allocate space for scrubbed environment. */
1209         for (count = 1, cpp = environ; *cpp; count++, cpp++)
1210                 continue;
1211         if ((new_environ = malloc(count * sizeof(char *))) == NULL) {
1212                 environ = NULL;
1213                 return;
1214         }
1215
1216         for (cpp2 = new_environ, cpp = environ; *cpp; cpp++) {
1217                 int reject_it = 0;
1218
1219                 for(p = rej; *p; p++)
1220                         if(strncmp(*cpp, *p, strlen(*p)) == 0) {
1221                                 reject_it = 1;
1222                                 break;
1223                         }
1224                 if (reject_it)
1225                         continue;
1226
1227                 for(p = acc; *p; p++)
1228                         if(strncmp(*cpp, *p, strlen(*p)) == 0)
1229                                 break;
1230                 if(*p != NULL) {
1231                         if ((*cpp2++ = strdup(*cpp)) == NULL) {
1232                                 environ = new_environ;
1233                                 return;
1234                         }
1235                 }
1236         }
1237         *cpp2 = NULL;
1238         environ = new_environ;
1239 }
1240
1241 /*
1242  * cleanup()
1243  *
1244  * This is the routine to call when we are all through, to
1245  * clean up anything that needs to be cleaned up.
1246  */
1247 /* ARGSUSED */
1248 void
1249 cleanup(int sig __unused)
1250 {
1251
1252         (void) shutdown(net, SHUT_RDWR);
1253         _exit(1);
1254 }