2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)sys_term.c 8.4+1 (Berkeley) 5/30/95
34 * $FreeBSD: src/libexec/telnetd/sys_term.c,v 1.24.2.8 2002/06/17 02:48:06 jmallett Exp $
35 * $DragonFly: src/libexec/telnetd/sys_term.c,v 1.2 2003/06/17 04:27:08 dillon Exp $
38 #include <sys/types.h>
45 #include "pathnames.h"
48 int cleanopen(char *);
54 char wtmpf[] = _PATH_WTMP;
56 char wtmpf[] = "/var/log/wtmp";
59 char utmpf[] = _PATH_UTMP;
61 char utmpf[] = "/var/run/utmp";
65 extern char **environ;
67 #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
68 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
95 # define cfsetospeed(tp, val) (tp)->sg.sg_ospeed = (val)
96 # define cfsetispeed(tp, val) (tp)->sg.sg_ispeed = (val)
97 # define cfgetospeed(tp) (tp)->sg.sg_ospeed
98 # define cfgetispeed(tp) (tp)->sg.sg_ispeed
99 #else /* USE_TERMIO */
102 # define TCSANOW TCSETS
103 # define TCSADRAIN TCSETSW
104 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
107 # define TCSANOW TCSETA
108 # define TCSADRAIN TCSETAW
109 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
111 # define TCSANOW TIOCSETA
112 # define TCSADRAIN TIOCSETAW
113 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
116 # define tcsetattr(f, a, t) ioctl(f, a, t)
117 # define cfsetospeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
118 (tp)->c_cflag |= (val)
119 # define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)
121 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CIBAUD; \
122 (tp)->c_cflag |= ((val)<<IBSHIFT)
123 # define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
125 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
126 (tp)->c_cflag |= (val)
127 # define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)
129 # endif /* TCSANOW */
130 struct termios termbuf, termbuf2; /* pty control structure */
131 #endif /* USE_TERMIO */
133 #include <sys/types.h>
136 int cleanopen(char *);
137 void scrub_env(void);
138 static char **addarg(char **, const char *);
145 * These three routines are used to get and set the "termbuf" structure
146 * to and from the kernel. init_termbuf() gets the current settings.
147 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
148 * set_termbuf() writes the structure into the kernel.
155 (void) ioctl(pty, TIOCGETP, (char *)&termbuf.sg);
156 (void) ioctl(pty, TIOCGETC, (char *)&termbuf.tc);
157 (void) ioctl(pty, TIOCGLTC, (char *)&termbuf.ltc);
159 (void) ioctl(pty, TIOCGSTATE, (char *)&termbuf.state);
162 (void) tcgetattr(pty, &termbuf);
167 #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
169 copy_termbuf(char *cp, size_t len)
171 if (len > sizeof(termbuf))
172 len = sizeof(termbuf);
173 memmove((char *)&termbuf, cp, len);
176 #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
182 * Only make the necessary changes.
185 if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg,
187 (void) ioctl(pty, TIOCSETN, (char *)&termbuf.sg);
188 if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc,
190 (void) ioctl(pty, TIOCSETC, (char *)&termbuf.tc);
191 if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc,
192 sizeof(termbuf.ltc)))
193 (void) ioctl(pty, TIOCSLTC, (char *)&termbuf.ltc);
194 if (termbuf.lflags != termbuf2.lflags)
195 (void) ioctl(pty, TIOCLSET, (char *)&termbuf.lflags);
196 #else /* USE_TERMIO */
197 if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
198 (void) tcsetattr(pty, TCSANOW, &termbuf);
199 #endif /* USE_TERMIO */
204 * spcset(func, valp, valpp)
206 * This function takes various special characters (func), and
207 * sets *valp to the current value of that character, and
208 * *valpp to point to where in the "termbuf" structure that
211 * It returns the SLC_ level of support for this function.
216 spcset(int func, cc_t *valp, cc_t **valpp)
220 *valp = termbuf.tc.t_eofc;
221 *valpp = (cc_t *)&termbuf.tc.t_eofc;
222 return(SLC_VARIABLE);
224 *valp = termbuf.sg.sg_erase;
225 *valpp = (cc_t *)&termbuf.sg.sg_erase;
226 return(SLC_VARIABLE);
228 *valp = termbuf.sg.sg_kill;
229 *valpp = (cc_t *)&termbuf.sg.sg_kill;
230 return(SLC_VARIABLE);
232 *valp = termbuf.tc.t_intrc;
233 *valpp = (cc_t *)&termbuf.tc.t_intrc;
234 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
236 *valp = termbuf.tc.t_quitc;
237 *valpp = (cc_t *)&termbuf.tc.t_quitc;
238 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
240 *valp = termbuf.tc.t_startc;
241 *valpp = (cc_t *)&termbuf.tc.t_startc;
242 return(SLC_VARIABLE);
244 *valp = termbuf.tc.t_stopc;
245 *valpp = (cc_t *)&termbuf.tc.t_stopc;
246 return(SLC_VARIABLE);
248 *valp = termbuf.ltc.t_flushc;
249 *valpp = (cc_t *)&termbuf.ltc.t_flushc;
250 return(SLC_VARIABLE);
252 *valp = termbuf.ltc.t_suspc;
253 *valpp = (cc_t *)&termbuf.ltc.t_suspc;
254 return(SLC_VARIABLE);
256 *valp = termbuf.ltc.t_werasc;
257 *valpp = (cc_t *)&termbuf.ltc.t_werasc;
258 return(SLC_VARIABLE);
260 *valp = termbuf.ltc.t_rprntc;
261 *valpp = (cc_t *)&termbuf.ltc.t_rprntc;
262 return(SLC_VARIABLE);
264 *valp = termbuf.ltc.t_lnextc;
265 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
266 return(SLC_VARIABLE);
268 *valp = termbuf.tc.t_brkc;
269 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
270 return(SLC_VARIABLE);
281 return(SLC_NOSUPPORT);
285 #else /* USE_TERMIO */
288 #define setval(a, b) *valp = termbuf.c_cc[a]; \
289 *valpp = &termbuf.c_cc[a]; \
291 #define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT);
294 spcset(int func, cc_t *valp, cc_t **valpp)
298 setval(VEOF, SLC_VARIABLE);
300 setval(VERASE, SLC_VARIABLE);
302 setval(VKILL, SLC_VARIABLE);
304 setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
306 setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
309 setval(VSTART, SLC_VARIABLE);
315 setval(VSTOP, SLC_VARIABLE);
321 setval(VWERASE, SLC_VARIABLE);
327 setval(VREPRINT, SLC_VARIABLE);
333 setval(VLNEXT, SLC_VARIABLE);
338 #if !defined(VDISCARD) && defined(VFLUSHO)
339 # define VDISCARD VFLUSHO
342 setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
348 setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
354 setval(VEOL, SLC_VARIABLE);
358 setval(VEOL2, SLC_VARIABLE);
362 setval(VSTATUS, SLC_VARIABLE);
375 return(SLC_NOSUPPORT);
378 #endif /* USE_TERMIO */
383 * Allocate a pty. As a side effect, the external character
384 * array "line" contains the name of the slave side.
386 * Returns the file descriptor of the opened pty.
388 char alpha[] = "0123456789abcdefghijklmnopqrstuv";
392 getpty(int *ptynum __unused)
399 (void) strcpy(line, _PATH_DEV);
400 (void) strcat(line, "ptyXX");
404 for (cp = "pqrsPQRS"; *cp; cp++) {
410 * This stat() check is just to keep us from
411 * looping through all 256 combinations if there
412 * aren't that many ptys available.
414 if (stat(line, &stb) < 0)
416 for (i = 0; i < 32; i++) {
432 * tty_flowmode() Find out if flow control is enabled or disabled.
433 * tty_linemode() Find out if linemode (external processing) is enabled.
434 * tty_setlinemod(on) Turn on/off linemode.
435 * tty_isecho() Find out if echoing is turned on.
436 * tty_setecho(on) Enable/disable character echoing.
437 * tty_israw() Find out if terminal is in RAW mode.
438 * tty_binaryin(on) Turn on/off BINARY on input.
439 * tty_binaryout(on) Turn on/off BINARY on output.
440 * tty_isediting() Find out if line editing is enabled.
441 * tty_istrapsig() Find out if signal trapping is enabled.
442 * tty_setedit(on) Turn on/off line editing.
443 * tty_setsig(on) Turn on/off signal trapping.
444 * tty_issofttab() Find out if tab expansion is enabled.
445 * tty_setsofttab(on) Turn on/off soft tab expansion.
446 * tty_islitecho() Find out if typed control chars are echoed literally
447 * tty_setlitecho() Turn on/off literal echo of control chars
448 * tty_tspeed(val) Set transmit speed to val.
449 * tty_rspeed(val) Set receive speed to val.
457 return(termbuf.state & TS_EXTPROC);
459 return(termbuf.c_lflag & EXTPROC);
464 tty_setlinemode(int on)
468 (void) ioctl(pty, TIOCEXT, (char *)&on);
473 termbuf.c_lflag |= EXTPROC;
475 termbuf.c_lflag &= ~EXTPROC;
479 #endif /* LINEMODE */
485 return (termbuf.sg.sg_flags & ECHO);
487 return (termbuf.c_lflag & ECHO);
495 return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0);
497 return((termbuf.c_iflag & IXON) ? 1 : 0);
506 return((termbuf.lflags & DECCTQ) ? 0 : 1);
511 return((termbuf.c_iflag & IXANY) ? 1 : 0);
520 termbuf.sg.sg_flags |= ECHO|CRMOD;
522 termbuf.sg.sg_flags &= ~(ECHO|CRMOD);
525 termbuf.c_lflag |= ECHO;
527 termbuf.c_lflag &= ~ECHO;
535 return(termbuf.sg.sg_flags & RAW);
537 return(!(termbuf.c_lflag & ICANON));
547 termbuf.lflags |= LPASS8;
549 termbuf.lflags &= ~LPASS8;
552 termbuf.c_iflag &= ~ISTRIP;
554 termbuf.c_iflag |= ISTRIP;
560 tty_binaryout(int on)
564 termbuf.lflags |= LLITOUT;
566 termbuf.lflags &= ~LLITOUT;
569 termbuf.c_cflag &= ~(CSIZE|PARENB);
570 termbuf.c_cflag |= CS8;
571 termbuf.c_oflag &= ~OPOST;
573 termbuf.c_cflag &= ~CSIZE;
574 termbuf.c_cflag |= CS7|PARENB;
575 termbuf.c_oflag |= OPOST;
584 return(termbuf.lflags & LPASS8);
586 return(!(termbuf.c_iflag & ISTRIP));
591 tty_isbinaryout(void)
594 return(termbuf.lflags & LLITOUT);
596 return(!(termbuf.c_oflag&OPOST));
605 return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));
607 return(termbuf.c_lflag & ICANON);
615 return(!(termbuf.sg.sg_flags&RAW));
617 return(termbuf.c_lflag & ISIG);
626 termbuf.sg.sg_flags &= ~CBREAK;
628 termbuf.sg.sg_flags |= CBREAK;
631 termbuf.c_lflag |= ICANON;
633 termbuf.c_lflag &= ~ICANON;
645 termbuf.c_lflag |= ISIG;
647 termbuf.c_lflag &= ~ISIG;
650 #endif /* LINEMODE */
656 return (termbuf.sg.sg_flags & XTABS);
659 return (termbuf.c_oflag & OXTABS);
662 return ((termbuf.c_oflag & TABDLY) == TAB3);
668 tty_setsofttab(int on)
672 termbuf.sg.sg_flags |= XTABS;
674 termbuf.sg.sg_flags &= ~XTABS;
678 termbuf.c_oflag |= OXTABS;
681 termbuf.c_oflag &= ~TABDLY;
682 termbuf.c_oflag |= TAB3;
686 termbuf.c_oflag &= ~OXTABS;
689 termbuf.c_oflag &= ~TABDLY;
690 termbuf.c_oflag |= TAB0;
700 return (!(termbuf.lflags & LCTLECH));
703 return (!(termbuf.c_lflag & ECHOCTL));
706 return (!(termbuf.c_lflag & TCTLECH));
708 # if !defined(ECHOCTL) && !defined(TCTLECH)
709 return (0); /* assumes ctl chars are echoed '^x' */
715 tty_setlitecho(int on)
719 termbuf.lflags &= ~LCTLECH;
721 termbuf.lflags |= LCTLECH;
725 termbuf.c_lflag &= ~ECHOCTL;
727 termbuf.c_lflag |= ECHOCTL;
731 termbuf.c_lflag &= ~TCTLECH;
733 termbuf.c_lflag |= TCTLECH;
742 return (termbuf.sg.sg_flags & CRMOD);
744 return (termbuf.c_iflag & ICRNL);
749 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
758 * A table of available terminal speeds
764 { 0, B0 }, { 50, B50 }, { 75, B75 },
765 { 110, B110 }, { 134, B134 }, { 150, B150 },
766 { 200, B200 }, { 300, B300 }, { 600, B600 },
767 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
796 #endif /* DECODE_BAUD */
802 struct termspeeds *tp;
804 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
806 if (tp->speed == -1) /* back up to last valid value */
808 cfsetospeed(&termbuf, tp->value);
809 #else /* DECODE_BAUD */
810 cfsetospeed(&termbuf, val);
811 #endif /* DECODE_BAUD */
818 struct termspeeds *tp;
820 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
822 if (tp->speed == -1) /* back up to last valid value */
824 cfsetispeed(&termbuf, tp->value);
825 #else /* DECODE_BAUD */
826 cfsetispeed(&termbuf, val);
827 #endif /* DECODE_BAUD */
833 * Open the slave side of the pty, and do any initialization
847 extern int def_row, def_col;
849 extern int def_tspeed, def_rspeed;
851 * Opening the slave side may cause initilization of the
852 * kernel tty structure. We need remember the state of
853 * if linemode was turned on
854 * terminal window size
857 * so that we can re-set them if we need to.
860 waslm = tty_linemode();
862 erase = termbuf.c_cc[VERASE];
865 * Make sure that we don't have a controlling tty, and
866 * that we are the session (process group) leader.
869 t = open(_PATH_TTY, O_RDWR);
871 (void) ioctl(t, TIOCNOTTY, (char *)0);
878 fatalperror(net, line);
882 * set up the tty modes as we like them to be.
886 if (def_row || def_col) {
887 memset((char *)&ws, 0, sizeof(ws));
890 (void)ioctl(t, TIOCSWINSZ, (char *)&ws);
895 * Settings for sgtty based systems
898 termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
899 # endif /* USE_TERMIO */
902 * Settings for all other termios/termio based
903 * systems, other than 4.4BSD. In 4.4BSD the
904 * kernel does the initial terminal setup.
906 tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
907 tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
909 termbuf.c_cc[VERASE] = erase;
913 # endif /* LINEMODE */
916 * Set the tty modes, and make this our controlling tty.
919 if (login_tty(t) == -1)
920 fatalperror(net, "login_tty");
929 * Open the specified slave side of the pty,
930 * making sure that we have a clean tty.
938 * Make sure that other people can't open the
939 * slave side of the connection.
941 (void) chown(li, 0, 0);
942 (void) chmod(li, 0600);
946 t = open(line, O_RDWR|O_NOCTTY);
957 * Given a hostname, do whatever
958 * is necessary to startup the login process on the slave side of the pty.
963 startslave(char *host, int autologin, char *autoname)
969 if ((i = fork()) < 0)
970 fatalperror(net, "fork");
974 start_login(host, autologin, autoname);
985 if ((*envp = getenv("TZ")))
995 * Assuming that we are now running as a child processes, this
996 * function will turn us into the login process.
999 #define undef1 __unused
1002 start_login(char *host undef1, int autologin undef1, char *name undef1)
1009 * -h : pass on name of host.
1010 * WARNING: -h is accepted by login if and only if
1012 * -p : don't clobber the environment (so terminal type stays set).
1014 * -f : force this login, he has already been authenticated
1016 argv = addarg(0, "login");
1018 #if !defined(NO_LOGIN_H)
1020 #if !defined(NO_LOGIN_P)
1021 argv = addarg(argv, "-p");
1025 * Set the environment variable "LINEMODE" to either
1026 * "real" or "kludge" if we are operating in either
1027 * real or kludge linemode.
1029 if (lmodetype == REAL_LINEMODE)
1030 setenv("LINEMODE", "real", 1);
1031 # ifdef KLUDGELINEMODE
1032 else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK)
1033 setenv("LINEMODE", "kludge", 1);
1038 * Are we working as the bftp daemon? If so, then ask login
1039 * to start bftp instead of shell.
1042 argv = addarg(argv, "-e");
1043 argv = addarg(argv, BFTPPATH);
1046 if (getenv("USER")) {
1047 argv = addarg(argv, "--");
1048 argv = addarg(argv, getenv("USER"));
1049 #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1052 for (cpp = environ; *cpp; cpp++)
1053 argv = addarg(argv, *cpp);
1057 * Assume that login will set the USER variable
1058 * correctly. For SysV systems, this means that
1059 * USER will no longer be set, just LOGNAME by
1060 * login. (The problem is that if the auto-login
1061 * fails, and the user then specifies a different
1062 * account name, he can get logged in with both
1063 * LOGNAME and USER in his environment, but the
1064 * USER value will be wrong.
1070 if (altlogin == NULL) {
1071 altlogin = _PATH_LOGIN;
1073 execv(altlogin, argv);
1075 syslog(LOG_ERR, "%s: %m", altlogin);
1076 fatalperror(net, altlogin);
1081 addarg(char **argv, const char *val)
1087 * 10 entries, a leading length, and a null
1089 argv = (char **)malloc(sizeof(*argv) * 12);
1092 *argv++ = (char *)10;
1095 for (cpp = argv; *cpp; cpp++)
1097 if (cpp == &argv[(long)argv[-1]]) {
1099 *argv = (char *)((long)(*argv) + 10);
1100 argv = (char **)realloc(argv, sizeof(*argv)*((long)(*argv) + 2));
1104 cpp = &argv[(long)argv[-1] - 10];
1106 *cpp++ = strdup(val);
1114 * We only accept the environment variables listed below.
1119 static const char *rej[] = {
1124 static const char *acc[] = {
1125 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
1138 for (cpp2 = cpp = environ; *cpp; cpp++) {
1141 for(p = rej; *p; p++)
1142 if(strncmp(*cpp, *p, strlen(*p)) == 0) {
1149 for(p = acc; *p; p++)
1150 if(strncmp(*cpp, *p, strlen(*p)) == 0)
1161 * This is the routine to call when we are all through, to
1162 * clean up anything that needs to be cleaned up.
1166 cleanup(int sig __unused)
1171 p = line + sizeof(_PATH_DEV) - 1;
1173 * Block all signals before clearing the utmp entry. We don't want to
1174 * be called again after calling logout() and then not add the wtmp
1175 * entry because of not finding the corresponding entry in utmp.
1178 sigprocmask(SIG_SETMASK, &mask, NULL);
1181 (void)chmod(line, 0666);
1182 (void)chown(line, 0, 0);
1184 (void)chmod(line, 0666);
1185 (void)chown(line, 0, 0);
1186 (void) shutdown(net, 2);