1 /* $NetBSD: sys_term.c,v 1.47 2013/06/28 15:48:02 christos Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
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.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)sys_term.c 8.4+1 (Berkeley) 5/30/95";
37 __RCSID("$NetBSD: sys_term.c,v 1.47 2013/06/28 15:48:02 christos Exp $");
42 #include "pathnames.h"
54 #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
55 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
57 struct termios termbuf
, termbuf2
; /* pty control structure */
59 void getptyslave(void);
60 int cleanopen(char *);
61 char **addarg(char **, const char *);
63 int getent(char *, char *);
64 char *getstr(const char *, char **);
66 extern void kerberos5_cleanup(void);
74 * These three routines are used to get and set the "termbuf" structure
75 * to and from the kernel. init_termbuf() gets the current settings.
76 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
77 * set_termbuf() writes the structure into the kernel.
83 (void) tcgetattr(pty
, &termbuf
);
87 #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
89 copy_termbuf(char *cp
, int len
)
91 if ((size_t)len
> sizeof(termbuf
))
92 len
= sizeof(termbuf
);
93 memmove((char *)&termbuf
, cp
, len
);
96 #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
102 * Only make the necessary changes.
104 if (memcmp((char *)&termbuf
, (char *)&termbuf2
, sizeof(termbuf
)))
105 (void) tcsetattr(pty
, TCSANOW
, &termbuf
);
110 * spcset(func, valp, valpp)
112 * This function takes various special characters (func), and
113 * sets *valp to the current value of that character, and
114 * *valpp to point to where in the "termbuf" structure that
117 * It returns the SLC_ level of support for this function.
122 spcset(int func
, cc_t
*valp
, cc_t
**valpp
)
125 #define setval(a, b) *valp = termbuf.c_cc[a]; \
126 *valpp = &termbuf.c_cc[a]; \
128 #define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT);
132 setval(VEOF
, SLC_VARIABLE
);
134 setval(VERASE
, SLC_VARIABLE
);
136 setval(VKILL
, SLC_VARIABLE
);
138 setval(VINTR
, SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
140 setval(VQUIT
, SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
142 setval(VSTART
, SLC_VARIABLE
);
144 setval(VSTOP
, SLC_VARIABLE
);
146 setval(VWERASE
, SLC_VARIABLE
);
148 setval(VREPRINT
, SLC_VARIABLE
);
150 setval(VLNEXT
, SLC_VARIABLE
);
152 setval(VDISCARD
, SLC_VARIABLE
|SLC_FLUSHOUT
);
154 setval(VSUSP
, SLC_VARIABLE
|SLC_FLUSHIN
);
156 setval(VEOL
, SLC_VARIABLE
);
158 setval(VEOL2
, SLC_VARIABLE
);
160 setval(VSTATUS
, SLC_VARIABLE
);
170 return(SLC_NOSUPPORT
);
178 * Allocate a pty. As a side effect, the external character
179 * array "line" contains the name of the slave side.
181 * Returns the file descriptor of the opened pty.
184 char *line
= NULL16STR
;
186 static char Xline
[] = NULL16STR
;
191 static int ptyslavefd
; /* for cleanopen() */
198 ptyfd
= openpty(ptynum
, &ptyslavefd
, line
, NULL
, NULL
);
207 * tty_flowmode() Find out if flow control is enabled or disabled.
208 * tty_linemode() Find out if linemode (external processing) is enabled.
209 * tty_setlinemod(on) Turn on/off linemode.
210 * tty_isecho() Find out if echoing is turned on.
211 * tty_setecho(on) Enable/disable character echoing.
212 * tty_israw() Find out if terminal is in RAW mode.
213 * tty_binaryin(on) Turn on/off BINARY on input.
214 * tty_binaryout(on) Turn on/off BINARY on output.
215 * tty_isediting() Find out if line editing is enabled.
216 * tty_istrapsig() Find out if signal trapping is enabled.
217 * tty_setedit(on) Turn on/off line editing.
218 * tty_setsig(on) Turn on/off signal trapping.
219 * tty_issofttab() Find out if tab expansion is enabled.
220 * tty_setsofttab(on) Turn on/off soft tab expansion.
221 * tty_islitecho() Find out if typed control chars are echoed literally
222 * tty_setlitecho() Turn on/off literal echo of control chars
223 * tty_tspeed(val) Set transmit speed to val.
224 * tty_rspeed(val) Set receive speed to val.
231 return(termbuf
.c_lflag
& EXTPROC
);
235 tty_setlinemode(int on
)
238 (void) ioctl(pty
, TIOCEXT
, (char *)&on
);
241 #endif /* LINEMODE */
246 return (termbuf
.c_lflag
& ECHO
);
252 return((termbuf
.c_iflag
& IXON
) ? 1 : 0);
258 return((termbuf
.c_iflag
& IXANY
) ? 1 : 0);
265 termbuf
.c_lflag
|= ECHO
;
267 termbuf
.c_lflag
&= ~ECHO
;
273 return(!(termbuf
.c_lflag
& ICANON
));
280 termbuf
.c_iflag
&= ~ISTRIP
;
282 termbuf
.c_iflag
|= ISTRIP
;
287 tty_binaryout(int on
)
290 termbuf
.c_cflag
&= ~(CSIZE
|PARENB
);
291 termbuf
.c_cflag
|= CS8
;
292 termbuf
.c_oflag
&= ~OPOST
;
294 termbuf
.c_cflag
&= ~CSIZE
;
295 termbuf
.c_cflag
|= CS7
|PARENB
;
296 termbuf
.c_oflag
|= OPOST
;
303 return(!(termbuf
.c_iflag
& ISTRIP
));
307 tty_isbinaryout(void)
309 return(!(termbuf
.c_oflag
&OPOST
));
316 return(termbuf
.c_lflag
& ICANON
);
322 return(termbuf
.c_lflag
& ISIG
);
329 termbuf
.c_lflag
|= ICANON
;
331 termbuf
.c_lflag
&= ~ICANON
;
338 termbuf
.c_lflag
|= ISIG
;
340 termbuf
.c_lflag
&= ~ISIG
;
342 #endif /* LINEMODE */
348 return (termbuf
.c_oflag
& OXTABS
);
351 return ((termbuf
.c_oflag
& TABDLY
) == TAB3
);
356 tty_setsofttab(int on
)
360 termbuf
.c_oflag
|= OXTABS
;
363 termbuf
.c_oflag
&= ~TABDLY
;
364 termbuf
.c_oflag
|= TAB3
;
368 termbuf
.c_oflag
&= ~OXTABS
;
371 termbuf
.c_oflag
&= ~TABDLY
;
372 termbuf
.c_oflag
|= TAB0
;
381 return (!(termbuf
.c_lflag
& ECHOCTL
));
384 return (!(termbuf
.c_lflag
& TCTLECH
));
386 # if !defined(ECHOCTL) && !defined(TCTLECH)
387 return (0); /* assumes ctl chars are echoed '^x' */
392 tty_setlitecho(int on
)
396 termbuf
.c_lflag
&= ~ECHOCTL
;
398 termbuf
.c_lflag
|= ECHOCTL
;
402 termbuf
.c_lflag
&= ~TCTLECH
;
404 termbuf
.c_lflag
|= TCTLECH
;
411 return (termbuf
.c_iflag
& ICRNL
);
417 cfsetospeed(&termbuf
, val
);
423 cfsetispeed(&termbuf
, val
);
432 * Open the slave side of the pty, and do any initialization
433 * that is necessary. The return value is a file descriptor
434 * for the slave side.
436 extern int def_tspeed
, def_rspeed
;
437 extern int def_row
, def_col
;
449 * Opening the slave side may cause initilization of the
450 * kernel tty structure. We need remember the state of
451 * if linemode was turned on
452 * terminal window size
454 * so that we can re-set them if we need to.
457 waslm
= tty_linemode();
461 * Make sure that we don't have a controlling tty, and
462 * that we are the session (process group) leader.
464 t
= open(_PATH_TTY
, O_RDWR
);
466 (void) ioctl(t
, TIOCNOTTY
, (char *)0);
474 fatalperror(net
, line
);
478 * set up the tty modes as we like them to be.
481 if (def_row
|| def_col
) {
482 memset((char *)&ws
, 0, sizeof(ws
));
485 (void)ioctl(t
, TIOCSWINSZ
, (char *)&ws
);
489 * Settings for sgtty based systems
493 * Settings for all other termios/termio based
494 * systems, other than 4.4BSD. In 4.4BSD the
495 * kernel does the initial terminal setup.
497 tty_rspeed((def_rspeed
> 0) ? def_rspeed
: 9600);
498 tty_tspeed((def_tspeed
> 0) ? def_tspeed
: 9600);
502 #endif /* LINEMODE */
505 * Set the tty modes, and make this our controlling tty.
508 if (login_tty(t
) == -1)
509 fatalperror(net
, "login_tty");
519 * Open the specified slave side of the pty,
520 * making sure that we have a clean tty.
523 cleanopen(char *ttyline
)
531 * Given a hostname, do whatever
532 * is necessary to startup the login process on the slave side of the pty.
537 startslave(char *host
, int autologin
, char *autoname
)
541 #ifdef AUTHENTICATION
542 if (!autoname
|| !autoname
[0])
545 if (autologin
< auth_level
) {
546 fatal(net
, "Authorization failed");
552 if ((i
= fork()) < 0)
553 fatalperror(net
, "fork");
557 start_login(host
, autologin
, autoname
);
570 if ((*envp
= getenv("TZ")))
580 * Assuming that we are now running as a child processes, this
581 * function will turn us into the login process.
583 extern char *gettyname
;
586 start_login(char *host
, int autologin
, char *name
)
589 #define TABBUFSIZ 512
590 char defent
[TABBUFSIZ
];
591 char defstrs
[TABBUFSIZ
];
593 const char *loginprog
= NULL
;
594 extern struct sockaddr_storage from
;
595 char buf
[sizeof(from
) * 4 + 1];
600 * -a : pass on the address of the host.
601 * -h : pass on name of host.
602 * WARNING: -h and -a are accepted by login
603 * if and only if getuid() == 0.
604 * -p : don't clobber the environment (so terminal type stays set).
606 * -f : force this login, he has already been authenticated
608 argv
= addarg(0, "login");
610 argv
= addarg(argv
, "-a");
611 (void)strvisx(buf
, (const char *)(const void *)&from
, sizeof(from
),
613 argv
= addarg(argv
, buf
);
615 argv
= addarg(argv
, "-h");
616 argv
= addarg(argv
, host
);
618 argv
= addarg(argv
, "-p");
621 * Set the environment variable "LINEMODE" to either
622 * "real" or "kludge" if we are operating in either
623 * real or kludge linemode.
625 if (lmodetype
== REAL_LINEMODE
)
626 setenv("LINEMODE", "real", 1);
627 # ifdef KLUDGELINEMODE
628 else if (lmodetype
== KLUDGE_LINEMODE
|| lmodetype
== KLUDGE_OK
)
629 setenv("LINEMODE", "kludge", 1);
634 * don't worry about the -f that might get sent.
635 * A -s is supposed to override it anyhow.
637 if (require_secure_login
)
638 argv
= addarg(argv
, "-s");
640 #ifdef AUTHENTICATION
641 if (auth_level
>= 0 && autologin
== AUTH_VALID
) {
642 argv
= addarg(argv
, "-f");
643 argv
= addarg(argv
, "--");
644 argv
= addarg(argv
, name
);
647 if (getenv("USER")) {
648 argv
= addarg(argv
, "--");
649 argv
= addarg(argv
, getenv("USER"));
651 * Assume that login will set the USER variable
652 * correctly. For SysV systems, this means that
653 * USER will no longer be set, just LOGNAME by
654 * login. (The problem is that if the auto-login
655 * fails, and the user then specifies a different
656 * account name, he can get logged in with both
657 * LOGNAME and USER in his environment, but the
658 * USER value will be wrong.
662 if (getent(defent
, gettyname
) == 1) {
665 loginprog
= getstr("lo", &cp
);
667 if (loginprog
== NULL
)
668 loginprog
= _PATH_LOGIN
;
671 * This sleep(1) is in here so that telnetd can
672 * finish up with the tty. There's a race condition
673 * the login banner message gets lost...
676 execv(loginprog
, argv
);
678 syslog(LOG_ERR
, "%s: %m", loginprog
);
679 fatalperror(net
, loginprog
);
684 addarg(char **argv
, const char *val
)
691 * 10 entries, a leading length, and a null
693 argv
= malloc(sizeof(*argv
) * 12);
696 *argv
++ = (char *)10;
699 for (cpp
= argv
; *cpp
; cpp
++)
701 if (cpp
== &argv
[(long)argv
[-1]]) {
703 nargv
= realloc(argv
, sizeof(*argv
) * ((long)(*argv
) + 10 + 2));
705 fatal(net
, "not enough memory");
709 *argv
= (char *)((long)(*argv
) + 10);
711 cpp
= &argv
[(long)argv
[-1] - 10];
713 *cpp
++ = __UNCONST(val
);
721 * We only accept the environment variables listed below.
727 static const char *reject
[] = {
732 static const char *acceptstr
[] = {
733 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
747 for (cpp2
= cpp
= environ
; *cpp
; cpp
++) {
750 for(p
= reject
; *p
; p
++)
751 if(strncmp(*cpp
, *p
, strlen(*p
)) == 0) {
758 for(p
= acceptstr
; *p
; p
++)
759 if(strncmp(*cpp
, *p
, strlen(*p
)) == 0)
770 * This is the routine to call when we are all through, to
771 * clean up anything that needs to be cleaned up.
779 p
= line
+ sizeof(_PATH_DEV
) - 1;
785 if (logoutx(p
, 0, DEAD_PROCESS
))
786 logwtmpx(p
, "", "", 0, DEAD_PROCESS
);
788 (void)chmod(line
, 0666);
789 (void)chown(line
, 0, 0);
791 (void)chmod(line
, 0666);
792 (void)chown(line
, 0, 0);
794 if (ttyaction(line
, "telnetd", "root"))
795 syslog(LOG_ERR
, "%s: ttyaction failed", line
);
796 (void) shutdown(net
, 2);