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/crypto/telnet/telnetd/sys_term.c,v 1.7.2.5 2002/06/17 02:48:02 jmallett Exp $
37 #include <sys/types.h>
44 #include "pathnames.h"
47 #include <libtelnet/auth.h>
50 int cleanopen(char *);
56 char wtmpf
[] = _PATH_WTMP
;
58 char wtmpf
[] = "/var/log/wtmp";
61 char utmpf
[] = _PATH_UTMP
;
63 char utmpf
[] = "/var/run/utmp";
67 extern char **environ
;
69 #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
70 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
97 # define cfsetospeed(tp, val) (tp)->sg.sg_ospeed = (val)
98 # define cfsetispeed(tp, val) (tp)->sg.sg_ispeed = (val)
99 # define cfgetospeed(tp) (tp)->sg.sg_ospeed
100 # define cfgetispeed(tp) (tp)->sg.sg_ispeed
101 #else /* USE_TERMIO */
104 # define TCSANOW TCSETS
105 # define TCSADRAIN TCSETSW
106 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
109 # define TCSANOW TCSETA
110 # define TCSADRAIN TCSETAW
111 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
113 # define TCSANOW TIOCSETA
114 # define TCSADRAIN TIOCSETAW
115 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
118 # define tcsetattr(f, a, t) ioctl(f, a, t)
119 # define cfsetospeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
120 (tp)->c_cflag |= (val)
121 # define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)
123 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CIBAUD; \
124 (tp)->c_cflag |= ((val)<<IBSHIFT)
125 # define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
127 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
128 (tp)->c_cflag |= (val)
129 # define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)
131 # endif /* TCSANOW */
132 struct termios termbuf
, termbuf2
; /* pty control structure */
133 #endif /* USE_TERMIO */
135 int cleanopen(char *);
136 void scrub_env(void);
137 static char **addarg(char **, const char *);
144 * These three routines are used to get and set the "termbuf" structure
145 * to and from the kernel. init_termbuf() gets the current settings.
146 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
147 * set_termbuf() writes the structure into the kernel.
154 (void) ioctl(pty
, TIOCGETP
, (char *)&termbuf
.sg
);
155 (void) ioctl(pty
, TIOCGETC
, (char *)&termbuf
.tc
);
156 (void) ioctl(pty
, TIOCGLTC
, (char *)&termbuf
.ltc
);
158 (void) ioctl(pty
, TIOCGSTATE
, (char *)&termbuf
.state
);
161 (void) tcgetattr(pty
, &termbuf
);
166 #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
168 copy_termbuf(char *cp
, size_t len
)
170 if (len
> sizeof(termbuf
))
171 len
= sizeof(termbuf
);
172 memmove((char *)&termbuf
, cp
, len
);
175 #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
181 * Only make the necessary changes.
184 if (memcmp((char *)&termbuf
.sg
, (char *)&termbuf2
.sg
,
186 (void) ioctl(pty
, TIOCSETN
, (char *)&termbuf
.sg
);
187 if (memcmp((char *)&termbuf
.tc
, (char *)&termbuf2
.tc
,
189 (void) ioctl(pty
, TIOCSETC
, (char *)&termbuf
.tc
);
190 if (memcmp((char *)&termbuf
.ltc
, (char *)&termbuf2
.ltc
,
191 sizeof(termbuf
.ltc
)))
192 (void) ioctl(pty
, TIOCSLTC
, (char *)&termbuf
.ltc
);
193 if (termbuf
.lflags
!= termbuf2
.lflags
)
194 (void) ioctl(pty
, TIOCLSET
, (char *)&termbuf
.lflags
);
195 #else /* USE_TERMIO */
196 if (memcmp((char *)&termbuf
, (char *)&termbuf2
, sizeof(termbuf
)))
197 (void) tcsetattr(pty
, TCSANOW
, &termbuf
);
198 #endif /* USE_TERMIO */
203 * spcset(func, valp, valpp)
205 * This function takes various special characters (func), and
206 * sets *valp to the current value of that character, and
207 * *valpp to point to where in the "termbuf" structure that
210 * It returns the SLC_ level of support for this function.
215 spcset(int func
, cc_t
*valp
, cc_t
**valpp
)
219 *valp
= termbuf
.tc
.t_eofc
;
220 *valpp
= (cc_t
*)&termbuf
.tc
.t_eofc
;
221 return(SLC_VARIABLE
);
223 *valp
= termbuf
.sg
.sg_erase
;
224 *valpp
= (cc_t
*)&termbuf
.sg
.sg_erase
;
225 return(SLC_VARIABLE
);
227 *valp
= termbuf
.sg
.sg_kill
;
228 *valpp
= (cc_t
*)&termbuf
.sg
.sg_kill
;
229 return(SLC_VARIABLE
);
231 *valp
= termbuf
.tc
.t_intrc
;
232 *valpp
= (cc_t
*)&termbuf
.tc
.t_intrc
;
233 return(SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
235 *valp
= termbuf
.tc
.t_quitc
;
236 *valpp
= (cc_t
*)&termbuf
.tc
.t_quitc
;
237 return(SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
239 *valp
= termbuf
.tc
.t_startc
;
240 *valpp
= (cc_t
*)&termbuf
.tc
.t_startc
;
241 return(SLC_VARIABLE
);
243 *valp
= termbuf
.tc
.t_stopc
;
244 *valpp
= (cc_t
*)&termbuf
.tc
.t_stopc
;
245 return(SLC_VARIABLE
);
247 *valp
= termbuf
.ltc
.t_flushc
;
248 *valpp
= (cc_t
*)&termbuf
.ltc
.t_flushc
;
249 return(SLC_VARIABLE
);
251 *valp
= termbuf
.ltc
.t_suspc
;
252 *valpp
= (cc_t
*)&termbuf
.ltc
.t_suspc
;
253 return(SLC_VARIABLE
);
255 *valp
= termbuf
.ltc
.t_werasc
;
256 *valpp
= (cc_t
*)&termbuf
.ltc
.t_werasc
;
257 return(SLC_VARIABLE
);
259 *valp
= termbuf
.ltc
.t_rprntc
;
260 *valpp
= (cc_t
*)&termbuf
.ltc
.t_rprntc
;
261 return(SLC_VARIABLE
);
263 *valp
= termbuf
.ltc
.t_lnextc
;
264 *valpp
= (cc_t
*)&termbuf
.ltc
.t_lnextc
;
265 return(SLC_VARIABLE
);
267 *valp
= termbuf
.tc
.t_brkc
;
268 *valpp
= (cc_t
*)&termbuf
.ltc
.t_lnextc
;
269 return(SLC_VARIABLE
);
280 return(SLC_NOSUPPORT
);
284 #else /* USE_TERMIO */
287 #define setval(a, b) *valp = termbuf.c_cc[a]; \
288 *valpp = &termbuf.c_cc[a]; \
290 #define defval(a) *valp = ((cc_t)a); *valpp = NULL; return(SLC_DEFAULT);
293 spcset(int func
, cc_t
*valp
, cc_t
**valpp
)
297 setval(VEOF
, SLC_VARIABLE
);
299 setval(VERASE
, SLC_VARIABLE
);
301 setval(VKILL
, SLC_VARIABLE
);
303 setval(VINTR
, SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
305 setval(VQUIT
, SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
308 setval(VSTART
, SLC_VARIABLE
);
314 setval(VSTOP
, SLC_VARIABLE
);
320 setval(VWERASE
, SLC_VARIABLE
);
326 setval(VREPRINT
, SLC_VARIABLE
);
332 setval(VLNEXT
, SLC_VARIABLE
);
337 #if !defined(VDISCARD) && defined(VFLUSHO)
338 # define VDISCARD VFLUSHO
341 setval(VDISCARD
, SLC_VARIABLE
|SLC_FLUSHOUT
);
347 setval(VSUSP
, SLC_VARIABLE
|SLC_FLUSHIN
);
353 setval(VEOL
, SLC_VARIABLE
);
357 setval(VEOL2
, SLC_VARIABLE
);
361 setval(VSTATUS
, SLC_VARIABLE
);
374 return(SLC_NOSUPPORT
);
377 #endif /* USE_TERMIO */
382 * Allocate a pty. As a side effect, the external character
383 * array "line" contains the name of the slave side.
385 * Returns the file descriptor of the opened pty.
387 char alpha
[] = "0123456789abcdefghijklmnopqrstuv";
391 getpty(int *ptynum __unused
)
398 (void) strcpy(line
, _PATH_DEV
);
399 (void) strcat(line
, "ptyXX");
403 for (cp
= "pqrsPQRS"; *cp
; cp
++) {
409 * This stat() check is just to keep us from
410 * looping through all 256 combinations if there
411 * aren't that many ptys available.
413 if (stat(line
, &stb
) < 0)
415 for (i
= 0; i
< 32; i
++) {
431 * tty_flowmode() Find out if flow control is enabled or disabled.
432 * tty_linemode() Find out if linemode (external processing) is enabled.
433 * tty_setlinemod(on) Turn on/off linemode.
434 * tty_isecho() Find out if echoing is turned on.
435 * tty_setecho(on) Enable/disable character echoing.
436 * tty_israw() Find out if terminal is in RAW mode.
437 * tty_binaryin(on) Turn on/off BINARY on input.
438 * tty_binaryout(on) Turn on/off BINARY on output.
439 * tty_isediting() Find out if line editing is enabled.
440 * tty_istrapsig() Find out if signal trapping is enabled.
441 * tty_setedit(on) Turn on/off line editing.
442 * tty_setsig(on) Turn on/off signal trapping.
443 * tty_issofttab() Find out if tab expansion is enabled.
444 * tty_setsofttab(on) Turn on/off soft tab expansion.
445 * tty_islitecho() Find out if typed control chars are echoed literally
446 * tty_setlitecho() Turn on/off literal echo of control chars
447 * tty_tspeed(val) Set transmit speed to val.
448 * tty_rspeed(val) Set receive speed to val.
456 return(termbuf
.state
& TS_EXTPROC
);
458 return(termbuf
.c_lflag
& EXTPROC
);
463 tty_setlinemode(int on
)
467 (void) ioctl(pty
, TIOCEXT
, (char *)&on
);
472 termbuf
.c_lflag
|= EXTPROC
;
474 termbuf
.c_lflag
&= ~EXTPROC
;
478 #endif /* LINEMODE */
484 return (termbuf
.sg
.sg_flags
& ECHO
);
486 return (termbuf
.c_lflag
& ECHO
);
494 return(((termbuf
.tc
.t_startc
) > 0 && (termbuf
.tc
.t_stopc
) > 0) ? 1 : 0);
496 return((termbuf
.c_iflag
& IXON
) ? 1 : 0);
505 return((termbuf
.lflags
& DECCTQ
) ? 0 : 1);
510 return((termbuf
.c_iflag
& IXANY
) ? 1 : 0);
519 termbuf
.sg
.sg_flags
|= ECHO
|CRMOD
;
521 termbuf
.sg
.sg_flags
&= ~(ECHO
|CRMOD
);
524 termbuf
.c_lflag
|= ECHO
;
526 termbuf
.c_lflag
&= ~ECHO
;
534 return(termbuf
.sg
.sg_flags
& RAW
);
536 return(!(termbuf
.c_lflag
& ICANON
));
540 #ifdef AUTHENTICATION
541 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
547 termbuf
.sg
.sg_flags
|= RAW
;
549 termbuf
.sg
.sg_flags
&= ~RAW
;
552 termbuf
.c_lflag
&= ~ICANON
;
554 termbuf
.c_lflag
|= ICANON
;
558 #endif /* AUTHENTICATION */
565 termbuf
.lflags
|= LPASS8
;
567 termbuf
.lflags
&= ~LPASS8
;
570 termbuf
.c_iflag
&= ~ISTRIP
;
572 termbuf
.c_iflag
|= ISTRIP
;
578 tty_binaryout(int on
)
582 termbuf
.lflags
|= LLITOUT
;
584 termbuf
.lflags
&= ~LLITOUT
;
587 termbuf
.c_cflag
&= ~(CSIZE
|PARENB
);
588 termbuf
.c_cflag
|= CS8
;
589 termbuf
.c_oflag
&= ~OPOST
;
591 termbuf
.c_cflag
&= ~CSIZE
;
592 termbuf
.c_cflag
|= CS7
|PARENB
;
593 termbuf
.c_oflag
|= OPOST
;
602 return(termbuf
.lflags
& LPASS8
);
604 return(!(termbuf
.c_iflag
& ISTRIP
));
609 tty_isbinaryout(void)
612 return(termbuf
.lflags
& LLITOUT
);
614 return(!(termbuf
.c_oflag
&OPOST
));
623 return(!(termbuf
.sg
.sg_flags
& (CBREAK
|RAW
)));
625 return(termbuf
.c_lflag
& ICANON
);
633 return(!(termbuf
.sg
.sg_flags
&RAW
));
635 return(termbuf
.c_lflag
& ISIG
);
644 termbuf
.sg
.sg_flags
&= ~CBREAK
;
646 termbuf
.sg
.sg_flags
|= CBREAK
;
649 termbuf
.c_lflag
|= ICANON
;
651 termbuf
.c_lflag
&= ~ICANON
;
663 termbuf
.c_lflag
|= ISIG
;
665 termbuf
.c_lflag
&= ~ISIG
;
668 #endif /* LINEMODE */
674 return (termbuf
.sg
.sg_flags
& XTABS
);
677 return (termbuf
.c_oflag
& OXTABS
);
680 return ((termbuf
.c_oflag
& TABDLY
) == TAB3
);
686 tty_setsofttab(int on
)
690 termbuf
.sg
.sg_flags
|= XTABS
;
692 termbuf
.sg
.sg_flags
&= ~XTABS
;
696 termbuf
.c_oflag
|= OXTABS
;
699 termbuf
.c_oflag
&= ~TABDLY
;
700 termbuf
.c_oflag
|= TAB3
;
704 termbuf
.c_oflag
&= ~OXTABS
;
707 termbuf
.c_oflag
&= ~TABDLY
;
708 termbuf
.c_oflag
|= TAB0
;
718 return (!(termbuf
.lflags
& LCTLECH
));
721 return (!(termbuf
.c_lflag
& ECHOCTL
));
724 return (!(termbuf
.c_lflag
& TCTLECH
));
726 # if !defined(ECHOCTL) && !defined(TCTLECH)
727 return (0); /* assumes ctl chars are echoed '^x' */
733 tty_setlitecho(int on
)
737 termbuf
.lflags
&= ~LCTLECH
;
739 termbuf
.lflags
|= LCTLECH
;
743 termbuf
.c_lflag
&= ~ECHOCTL
;
745 termbuf
.c_lflag
|= ECHOCTL
;
749 termbuf
.c_lflag
&= ~TCTLECH
;
751 termbuf
.c_lflag
|= TCTLECH
;
760 return (termbuf
.sg
.sg_flags
& CRMOD
);
762 return (termbuf
.c_iflag
& ICRNL
);
767 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
776 * A table of available terminal speeds
782 { 0, B0
}, { 50, B50
}, { 75, B75
},
783 { 110, B110
}, { 134, B134
}, { 150, B150
},
784 { 200, B200
}, { 300, B300
}, { 600, B600
},
785 { 1200, B1200
}, { 1800, B1800
}, { 2400, B2400
},
814 #endif /* DECODE_BAUD */
820 struct termspeeds
*tp
;
822 for (tp
= termspeeds
; (tp
->speed
!= -1) && (val
> tp
->speed
); tp
++)
824 if (tp
->speed
== -1) /* back up to last valid value */
826 cfsetospeed(&termbuf
, tp
->value
);
827 #else /* DECODE_BAUD */
828 cfsetospeed(&termbuf
, val
);
829 #endif /* DECODE_BAUD */
836 struct termspeeds
*tp
;
838 for (tp
= termspeeds
; (tp
->speed
!= -1) && (val
> tp
->speed
); tp
++)
840 if (tp
->speed
== -1) /* back up to last valid value */
842 cfsetispeed(&termbuf
, tp
->value
);
843 #else /* DECODE_BAUD */
844 cfsetispeed(&termbuf
, val
);
845 #endif /* DECODE_BAUD */
851 * Open the slave side of the pty, and do any initialization
865 extern int def_row
, def_col
;
867 extern int def_tspeed
, def_rspeed
;
869 * Opening the slave side may cause initilization of the
870 * kernel tty structure. We need remember the state of
871 * if linemode was turned on
872 * terminal window size
875 * so that we can re-set them if we need to.
878 waslm
= tty_linemode();
880 erase
= termbuf
.c_cc
[VERASE
];
883 * Make sure that we don't have a controlling tty, and
884 * that we are the session (process group) leader.
887 t
= open(_PATH_TTY
, O_RDWR
);
889 (void) ioctl(t
, TIOCNOTTY
, NULL
);
896 fatalperror(net
, line
);
900 * set up the tty modes as we like them to be.
904 if (def_row
|| def_col
) {
905 memset((char *)&ws
, 0, sizeof(ws
));
908 (void)ioctl(t
, TIOCSWINSZ
, (char *)&ws
);
913 * Settings for sgtty based systems
916 termbuf
.sg
.sg_flags
|= CRMOD
|ANYP
|ECHO
|XTABS
;
917 # endif /* USE_TERMIO */
920 * Settings for all other termios/termio based
921 * systems, other than 4.4BSD. In 4.4BSD the
922 * kernel does the initial terminal setup.
924 tty_rspeed((def_rspeed
> 0) ? def_rspeed
: 9600);
925 tty_tspeed((def_tspeed
> 0) ? def_tspeed
: 9600);
927 termbuf
.c_cc
[VERASE
] = erase
;
931 # endif /* LINEMODE */
934 * Set the tty modes, and make this our controlling tty.
937 if (login_tty(t
) == -1)
938 fatalperror(net
, "login_tty");
941 #ifdef AUTHENTICATION
942 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
944 * Leave the pty open so that we can write out the rlogin
945 * protocol for /bin/login, if the authentication works.
953 #endif /* AUTHENTICATION */
960 * Open the specified slave side of the pty,
961 * making sure that we have a clean tty.
969 * Make sure that other people can't open the
970 * slave side of the connection.
972 (void) chown(li
, 0, 0);
973 (void) chmod(li
, 0600);
977 t
= open(line
, O_RDWR
|O_NOCTTY
);
988 * Given a hostname, do whatever
989 * is necessary to startup the login process on the slave side of the pty.
994 startslave(char *host
, int autologin
, char *autoname
)
998 #ifdef AUTHENTICATION
999 if (!autoname
|| !autoname
[0])
1002 if (autologin
< auth_level
) {
1003 fatal(net
, "Authorization failed");
1009 if ((i
= fork()) < 0)
1010 fatalperror(net
, "fork");
1014 start_login(host
, autologin
, autoname
);
1025 if ((*envp
= getenv("TZ")))
1035 * Assuming that we are now running as a child processes, this
1036 * function will turn us into the login process.
1039 #ifndef AUTHENTICATION
1040 #define undef1 __unused
1046 start_login(char *host undef1
, int autologin undef1
, char *name undef1
)
1053 * -h : pass on name of host.
1054 * WARNING: -h is accepted by login if and only if
1056 * -p : don't clobber the environment (so terminal type stays set).
1058 * -f : force this login, he has already been authenticated
1060 argv
= addarg(0, "login");
1062 #if !defined(NO_LOGIN_H)
1063 #ifdef AUTHENTICATION
1064 # if defined(NO_LOGIN_F) && defined(LOGIN_R)
1066 * Don't add the "-h host" option if we are going
1067 * to be adding the "-r host" option down below...
1069 if ((auth_level
< 0) || (autologin
!= AUTH_VALID
))
1072 argv
= addarg(argv
, "-h");
1073 argv
= addarg(argv
, host
);
1075 #endif /* AUTHENTICATION */
1077 #if !defined(NO_LOGIN_P)
1078 argv
= addarg(argv
, "-p");
1082 * Set the environment variable "LINEMODE" to either
1083 * "real" or "kludge" if we are operating in either
1084 * real or kludge linemode.
1086 if (lmodetype
== REAL_LINEMODE
) {
1087 if (setenv("LINEMODE", "real", 1) == -1)
1088 syslog(LOG_ERR
, "setenv: cannot set LINEMODE=real: %m");
1090 # ifdef KLUDGELINEMODE
1091 else if (lmodetype
== KLUDGE_LINEMODE
|| lmodetype
== KLUDGE_OK
) {
1092 if (setenv("LINEMODE", "kludge", 1) == -1)
1093 syslog(LOG_ERR
, "setenv: cannot set LINEMODE=kludge: %m");
1099 * Are we working as the bftp daemon? If so, then ask login
1100 * to start bftp instead of shell.
1103 argv
= addarg(argv
, "-e");
1104 argv
= addarg(argv
, BFTPPATH
);
1107 #ifdef AUTHENTICATION
1108 if (auth_level
>= 0 && autologin
== AUTH_VALID
) {
1109 # if !defined(NO_LOGIN_F)
1110 argv
= addarg(argv
, "-f");
1111 argv
= addarg(argv
, "--");
1112 argv
= addarg(argv
, name
);
1114 # if defined(LOGIN_R)
1116 * We don't have support for "login -f", but we
1117 * can fool /bin/login into thinking that we are
1118 * rlogind, and allow us to log in without a
1119 * password. The rlogin protocol expects
1120 * local-user\0remote-user\0term/speed\0
1126 int isecho
, israw
, xpty
, len
;
1127 extern int def_rspeed
;
1130 * Tell login that we are coming from "localhost".
1131 * If we passed in the real host name, then the
1132 * user would have to allow .rhost access from
1133 * every machine that they want authenticated
1134 * access to work from, which sort of defeats
1135 * the purpose of an authenticated login...
1136 * So, we tell login that the session is coming
1137 * from "localhost", and the user will only have
1138 * to have "localhost" in their .rhost file.
1140 # define LOGIN_HOST "localhost"
1142 argv
= addarg(argv
, "-r");
1143 argv
= addarg(argv
, LOGIN_HOST
);
1148 isecho
= tty_isecho();
1149 israw
= tty_israw();
1150 if (isecho
|| !israw
) {
1151 tty_setecho(0); /* Turn off echo */
1152 tty_setraw(1); /* Turn on raw */
1155 len
= strlen(name
)+1;
1156 write(xpty
, name
, len
);
1157 write(xpty
, name
, len
);
1158 snprintf(speed
, sizeof(speed
),
1159 "%s/%d", (cp
= getenv("TERM")) ? cp
: "",
1160 (def_rspeed
> 0) ? def_rspeed
: 9600);
1161 len
= strlen(speed
)+1;
1162 write(xpty
, speed
, len
);
1164 if (isecho
|| !israw
) {
1166 tty_setecho(isecho
);
1171 * Write a newline to ensure
1172 * that login will be able to
1175 write(xpty
, "\n", 1);
1181 argv
= addarg(argv
, "--");
1182 argv
= addarg(argv
, name
);
1187 if (getenv("USER")) {
1188 argv
= addarg(argv
, "--");
1189 argv
= addarg(argv
, getenv("USER"));
1190 #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1193 for (cpp
= environ
; *cpp
; cpp
++)
1194 argv
= addarg(argv
, *cpp
);
1198 * Assume that login will set the USER variable
1199 * correctly. For SysV systems, this means that
1200 * USER will no longer be set, just LOGNAME by
1201 * login. (The problem is that if the auto-login
1202 * fails, and the user then specifies a different
1203 * account name, he can get logged in with both
1204 * LOGNAME and USER in his environment, but the
1205 * USER value will be wrong.
1209 #ifdef AUTHENTICATION
1210 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
1214 #endif /* AUTHENTICATION */
1217 if (altlogin
== NULL
) {
1218 altlogin
= _PATH_LOGIN
;
1220 execv(altlogin
, argv
);
1222 syslog(LOG_ERR
, "%s: %m", altlogin
);
1223 fatalperror(net
, altlogin
);
1228 addarg(char **argv
, const char *val
)
1234 * 10 entries, a leading length, and a null
1236 argv
= (char **)malloc(sizeof(*argv
) * 12);
1239 *argv
++ = (char *)10;
1242 for (cpp
= argv
; *cpp
; cpp
++)
1244 if (cpp
== &argv
[(long)argv
[-1]]) {
1246 *argv
= (char *)((long)(*argv
) + 10);
1247 argv
= (char **)realloc(argv
, sizeof(*argv
)*((long)(*argv
) + 2));
1251 cpp
= &argv
[(long)argv
[-1] - 10];
1253 *cpp
++ = strdup(val
);
1261 * We only accept the environment variables listed below.
1266 static const char *rej
[] = {
1271 static const char *acc
[] = {
1272 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
1284 char ** new_environ
;
1287 /* Allocate space for scrubbed environment. */
1288 for (count
= 1, cpp
= environ
; *cpp
; count
++, cpp
++)
1290 if ((new_environ
= malloc(count
* sizeof(char *))) == NULL
) {
1295 for (cpp2
= new_environ
, cpp
= environ
; *cpp
; cpp
++) {
1298 for(p
= rej
; *p
; p
++)
1299 if(strncmp(*cpp
, *p
, strlen(*p
)) == 0) {
1306 for(p
= acc
; *p
; p
++)
1307 if(strncmp(*cpp
, *p
, strlen(*p
)) == 0)
1310 if ((*cpp2
++ = strdup(*cpp
)) == NULL
) {
1311 environ
= new_environ
;
1317 environ
= new_environ
;
1323 * This is the routine to call when we are all through, to
1324 * clean up anything that needs to be cleaned up.
1328 cleanup(int sig __unused
)
1333 p
= line
+ sizeof(_PATH_DEV
) - 1;
1335 * Block all signals before clearing the utmp entry. We don't want to
1336 * be called again after calling logout() and then not add the wtmp
1337 * entry because of not finding the corresponding entry in utmp.
1340 sigprocmask(SIG_SETMASK
, &mask
, NULL
);
1343 (void)chmod(line
, 0666);
1344 (void)chown(line
, 0, 0);
1346 (void)chmod(line
, 0666);
1347 (void)chown(line
, 0, 0);
1348 (void) shutdown(net
, SHUT_RDWR
);