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 $
35 * $DragonFly: src/crypto/telnet/telnetd/sys_term.c,v 1.2 2003/06/17 04:24:37 dillon Exp $
38 #include <sys/types.h>
45 #include "pathnames.h"
48 #include <libtelnet/auth.h>
51 int cleanopen(char *);
57 char wtmpf
[] = _PATH_WTMP
;
59 char wtmpf
[] = "/var/log/wtmp";
62 char utmpf
[] = _PATH_UTMP
;
64 char utmpf
[] = "/var/run/utmp";
68 extern char **environ
;
70 #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
71 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
98 # define cfsetospeed(tp, val) (tp)->sg.sg_ospeed = (val)
99 # define cfsetispeed(tp, val) (tp)->sg.sg_ispeed = (val)
100 # define cfgetospeed(tp) (tp)->sg.sg_ospeed
101 # define cfgetispeed(tp) (tp)->sg.sg_ispeed
102 #else /* USE_TERMIO */
105 # define TCSANOW TCSETS
106 # define TCSADRAIN TCSETSW
107 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
110 # define TCSANOW TCSETA
111 # define TCSADRAIN TCSETAW
112 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
114 # define TCSANOW TIOCSETA
115 # define TCSADRAIN TIOCSETAW
116 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
119 # define tcsetattr(f, a, t) ioctl(f, a, t)
120 # define cfsetospeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
121 (tp)->c_cflag |= (val)
122 # define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)
124 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CIBAUD; \
125 (tp)->c_cflag |= ((val)<<IBSHIFT)
126 # define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
128 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
129 (tp)->c_cflag |= (val)
130 # define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)
132 # endif /* TCSANOW */
133 struct termios termbuf
, termbuf2
; /* pty control structure */
134 #endif /* USE_TERMIO */
136 #include <sys/types.h>
139 int cleanopen(char *);
140 void scrub_env(void);
141 static char **addarg(char **, const char *);
148 * These three routines are used to get and set the "termbuf" structure
149 * to and from the kernel. init_termbuf() gets the current settings.
150 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
151 * set_termbuf() writes the structure into the kernel.
158 (void) ioctl(pty
, TIOCGETP
, (char *)&termbuf
.sg
);
159 (void) ioctl(pty
, TIOCGETC
, (char *)&termbuf
.tc
);
160 (void) ioctl(pty
, TIOCGLTC
, (char *)&termbuf
.ltc
);
162 (void) ioctl(pty
, TIOCGSTATE
, (char *)&termbuf
.state
);
165 (void) tcgetattr(pty
, &termbuf
);
170 #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
172 copy_termbuf(char *cp
, size_t len
)
174 if (len
> sizeof(termbuf
))
175 len
= sizeof(termbuf
);
176 memmove((char *)&termbuf
, cp
, len
);
179 #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
185 * Only make the necessary changes.
188 if (memcmp((char *)&termbuf
.sg
, (char *)&termbuf2
.sg
,
190 (void) ioctl(pty
, TIOCSETN
, (char *)&termbuf
.sg
);
191 if (memcmp((char *)&termbuf
.tc
, (char *)&termbuf2
.tc
,
193 (void) ioctl(pty
, TIOCSETC
, (char *)&termbuf
.tc
);
194 if (memcmp((char *)&termbuf
.ltc
, (char *)&termbuf2
.ltc
,
195 sizeof(termbuf
.ltc
)))
196 (void) ioctl(pty
, TIOCSLTC
, (char *)&termbuf
.ltc
);
197 if (termbuf
.lflags
!= termbuf2
.lflags
)
198 (void) ioctl(pty
, TIOCLSET
, (char *)&termbuf
.lflags
);
199 #else /* USE_TERMIO */
200 if (memcmp((char *)&termbuf
, (char *)&termbuf2
, sizeof(termbuf
)))
201 (void) tcsetattr(pty
, TCSANOW
, &termbuf
);
202 #endif /* USE_TERMIO */
207 * spcset(func, valp, valpp)
209 * This function takes various special characters (func), and
210 * sets *valp to the current value of that character, and
211 * *valpp to point to where in the "termbuf" structure that
214 * It returns the SLC_ level of support for this function.
219 spcset(int func
, cc_t
*valp
, cc_t
**valpp
)
223 *valp
= termbuf
.tc
.t_eofc
;
224 *valpp
= (cc_t
*)&termbuf
.tc
.t_eofc
;
225 return(SLC_VARIABLE
);
227 *valp
= termbuf
.sg
.sg_erase
;
228 *valpp
= (cc_t
*)&termbuf
.sg
.sg_erase
;
229 return(SLC_VARIABLE
);
231 *valp
= termbuf
.sg
.sg_kill
;
232 *valpp
= (cc_t
*)&termbuf
.sg
.sg_kill
;
233 return(SLC_VARIABLE
);
235 *valp
= termbuf
.tc
.t_intrc
;
236 *valpp
= (cc_t
*)&termbuf
.tc
.t_intrc
;
237 return(SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
239 *valp
= termbuf
.tc
.t_quitc
;
240 *valpp
= (cc_t
*)&termbuf
.tc
.t_quitc
;
241 return(SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
243 *valp
= termbuf
.tc
.t_startc
;
244 *valpp
= (cc_t
*)&termbuf
.tc
.t_startc
;
245 return(SLC_VARIABLE
);
247 *valp
= termbuf
.tc
.t_stopc
;
248 *valpp
= (cc_t
*)&termbuf
.tc
.t_stopc
;
249 return(SLC_VARIABLE
);
251 *valp
= termbuf
.ltc
.t_flushc
;
252 *valpp
= (cc_t
*)&termbuf
.ltc
.t_flushc
;
253 return(SLC_VARIABLE
);
255 *valp
= termbuf
.ltc
.t_suspc
;
256 *valpp
= (cc_t
*)&termbuf
.ltc
.t_suspc
;
257 return(SLC_VARIABLE
);
259 *valp
= termbuf
.ltc
.t_werasc
;
260 *valpp
= (cc_t
*)&termbuf
.ltc
.t_werasc
;
261 return(SLC_VARIABLE
);
263 *valp
= termbuf
.ltc
.t_rprntc
;
264 *valpp
= (cc_t
*)&termbuf
.ltc
.t_rprntc
;
265 return(SLC_VARIABLE
);
267 *valp
= termbuf
.ltc
.t_lnextc
;
268 *valpp
= (cc_t
*)&termbuf
.ltc
.t_lnextc
;
269 return(SLC_VARIABLE
);
271 *valp
= termbuf
.tc
.t_brkc
;
272 *valpp
= (cc_t
*)&termbuf
.ltc
.t_lnextc
;
273 return(SLC_VARIABLE
);
284 return(SLC_NOSUPPORT
);
288 #else /* USE_TERMIO */
291 #define setval(a, b) *valp = termbuf.c_cc[a]; \
292 *valpp = &termbuf.c_cc[a]; \
294 #define defval(a) *valp = ((cc_t)a); *valpp = NULL; return(SLC_DEFAULT);
297 spcset(int func
, cc_t
*valp
, cc_t
**valpp
)
301 setval(VEOF
, SLC_VARIABLE
);
303 setval(VERASE
, SLC_VARIABLE
);
305 setval(VKILL
, SLC_VARIABLE
);
307 setval(VINTR
, SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
309 setval(VQUIT
, SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
312 setval(VSTART
, SLC_VARIABLE
);
318 setval(VSTOP
, SLC_VARIABLE
);
324 setval(VWERASE
, SLC_VARIABLE
);
330 setval(VREPRINT
, SLC_VARIABLE
);
336 setval(VLNEXT
, SLC_VARIABLE
);
341 #if !defined(VDISCARD) && defined(VFLUSHO)
342 # define VDISCARD VFLUSHO
345 setval(VDISCARD
, SLC_VARIABLE
|SLC_FLUSHOUT
);
351 setval(VSUSP
, SLC_VARIABLE
|SLC_FLUSHIN
);
357 setval(VEOL
, SLC_VARIABLE
);
361 setval(VEOL2
, SLC_VARIABLE
);
365 setval(VSTATUS
, SLC_VARIABLE
);
378 return(SLC_NOSUPPORT
);
381 #endif /* USE_TERMIO */
386 * Allocate a pty. As a side effect, the external character
387 * array "line" contains the name of the slave side.
389 * Returns the file descriptor of the opened pty.
391 char alpha
[] = "0123456789abcdefghijklmnopqrstuv";
395 getpty(int *ptynum __unused
)
402 (void) strcpy(line
, _PATH_DEV
);
403 (void) strcat(line
, "ptyXX");
407 for (cp
= "pqrsPQRS"; *cp
; cp
++) {
413 * This stat() check is just to keep us from
414 * looping through all 256 combinations if there
415 * aren't that many ptys available.
417 if (stat(line
, &stb
) < 0)
419 for (i
= 0; i
< 32; i
++) {
435 * tty_flowmode() Find out if flow control is enabled or disabled.
436 * tty_linemode() Find out if linemode (external processing) is enabled.
437 * tty_setlinemod(on) Turn on/off linemode.
438 * tty_isecho() Find out if echoing is turned on.
439 * tty_setecho(on) Enable/disable character echoing.
440 * tty_israw() Find out if terminal is in RAW mode.
441 * tty_binaryin(on) Turn on/off BINARY on input.
442 * tty_binaryout(on) Turn on/off BINARY on output.
443 * tty_isediting() Find out if line editing is enabled.
444 * tty_istrapsig() Find out if signal trapping is enabled.
445 * tty_setedit(on) Turn on/off line editing.
446 * tty_setsig(on) Turn on/off signal trapping.
447 * tty_issofttab() Find out if tab expansion is enabled.
448 * tty_setsofttab(on) Turn on/off soft tab expansion.
449 * tty_islitecho() Find out if typed control chars are echoed literally
450 * tty_setlitecho() Turn on/off literal echo of control chars
451 * tty_tspeed(val) Set transmit speed to val.
452 * tty_rspeed(val) Set receive speed to val.
460 return(termbuf
.state
& TS_EXTPROC
);
462 return(termbuf
.c_lflag
& EXTPROC
);
467 tty_setlinemode(int on
)
471 (void) ioctl(pty
, TIOCEXT
, (char *)&on
);
476 termbuf
.c_lflag
|= EXTPROC
;
478 termbuf
.c_lflag
&= ~EXTPROC
;
482 #endif /* LINEMODE */
488 return (termbuf
.sg
.sg_flags
& ECHO
);
490 return (termbuf
.c_lflag
& ECHO
);
498 return(((termbuf
.tc
.t_startc
) > 0 && (termbuf
.tc
.t_stopc
) > 0) ? 1 : 0);
500 return((termbuf
.c_iflag
& IXON
) ? 1 : 0);
509 return((termbuf
.lflags
& DECCTQ
) ? 0 : 1);
514 return((termbuf
.c_iflag
& IXANY
) ? 1 : 0);
523 termbuf
.sg
.sg_flags
|= ECHO
|CRMOD
;
525 termbuf
.sg
.sg_flags
&= ~(ECHO
|CRMOD
);
528 termbuf
.c_lflag
|= ECHO
;
530 termbuf
.c_lflag
&= ~ECHO
;
538 return(termbuf
.sg
.sg_flags
& RAW
);
540 return(!(termbuf
.c_lflag
& ICANON
));
544 #ifdef AUTHENTICATION
545 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
551 termbuf
.sg
.sg_flags
|= RAW
;
553 termbuf
.sg
.sg_flags
&= ~RAW
;
556 termbuf
.c_lflag
&= ~ICANON
;
558 termbuf
.c_lflag
|= ICANON
;
562 #endif /* AUTHENTICATION */
569 termbuf
.lflags
|= LPASS8
;
571 termbuf
.lflags
&= ~LPASS8
;
574 termbuf
.c_iflag
&= ~ISTRIP
;
576 termbuf
.c_iflag
|= ISTRIP
;
582 tty_binaryout(int on
)
586 termbuf
.lflags
|= LLITOUT
;
588 termbuf
.lflags
&= ~LLITOUT
;
591 termbuf
.c_cflag
&= ~(CSIZE
|PARENB
);
592 termbuf
.c_cflag
|= CS8
;
593 termbuf
.c_oflag
&= ~OPOST
;
595 termbuf
.c_cflag
&= ~CSIZE
;
596 termbuf
.c_cflag
|= CS7
|PARENB
;
597 termbuf
.c_oflag
|= OPOST
;
606 return(termbuf
.lflags
& LPASS8
);
608 return(!(termbuf
.c_iflag
& ISTRIP
));
613 tty_isbinaryout(void)
616 return(termbuf
.lflags
& LLITOUT
);
618 return(!(termbuf
.c_oflag
&OPOST
));
627 return(!(termbuf
.sg
.sg_flags
& (CBREAK
|RAW
)));
629 return(termbuf
.c_lflag
& ICANON
);
637 return(!(termbuf
.sg
.sg_flags
&RAW
));
639 return(termbuf
.c_lflag
& ISIG
);
648 termbuf
.sg
.sg_flags
&= ~CBREAK
;
650 termbuf
.sg
.sg_flags
|= CBREAK
;
653 termbuf
.c_lflag
|= ICANON
;
655 termbuf
.c_lflag
&= ~ICANON
;
667 termbuf
.c_lflag
|= ISIG
;
669 termbuf
.c_lflag
&= ~ISIG
;
672 #endif /* LINEMODE */
678 return (termbuf
.sg
.sg_flags
& XTABS
);
681 return (termbuf
.c_oflag
& OXTABS
);
684 return ((termbuf
.c_oflag
& TABDLY
) == TAB3
);
690 tty_setsofttab(int on
)
694 termbuf
.sg
.sg_flags
|= XTABS
;
696 termbuf
.sg
.sg_flags
&= ~XTABS
;
700 termbuf
.c_oflag
|= OXTABS
;
703 termbuf
.c_oflag
&= ~TABDLY
;
704 termbuf
.c_oflag
|= TAB3
;
708 termbuf
.c_oflag
&= ~OXTABS
;
711 termbuf
.c_oflag
&= ~TABDLY
;
712 termbuf
.c_oflag
|= TAB0
;
722 return (!(termbuf
.lflags
& LCTLECH
));
725 return (!(termbuf
.c_lflag
& ECHOCTL
));
728 return (!(termbuf
.c_lflag
& TCTLECH
));
730 # if !defined(ECHOCTL) && !defined(TCTLECH)
731 return (0); /* assumes ctl chars are echoed '^x' */
737 tty_setlitecho(int on
)
741 termbuf
.lflags
&= ~LCTLECH
;
743 termbuf
.lflags
|= LCTLECH
;
747 termbuf
.c_lflag
&= ~ECHOCTL
;
749 termbuf
.c_lflag
|= ECHOCTL
;
753 termbuf
.c_lflag
&= ~TCTLECH
;
755 termbuf
.c_lflag
|= TCTLECH
;
764 return (termbuf
.sg
.sg_flags
& CRMOD
);
766 return (termbuf
.c_iflag
& ICRNL
);
771 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
780 * A table of available terminal speeds
786 { 0, B0
}, { 50, B50
}, { 75, B75
},
787 { 110, B110
}, { 134, B134
}, { 150, B150
},
788 { 200, B200
}, { 300, B300
}, { 600, B600
},
789 { 1200, B1200
}, { 1800, B1800
}, { 2400, B2400
},
818 #endif /* DECODE_BAUD */
824 struct termspeeds
*tp
;
826 for (tp
= termspeeds
; (tp
->speed
!= -1) && (val
> tp
->speed
); tp
++)
828 if (tp
->speed
== -1) /* back up to last valid value */
830 cfsetospeed(&termbuf
, tp
->value
);
831 #else /* DECODE_BAUD */
832 cfsetospeed(&termbuf
, val
);
833 #endif /* DECODE_BAUD */
840 struct termspeeds
*tp
;
842 for (tp
= termspeeds
; (tp
->speed
!= -1) && (val
> tp
->speed
); tp
++)
844 if (tp
->speed
== -1) /* back up to last valid value */
846 cfsetispeed(&termbuf
, tp
->value
);
847 #else /* DECODE_BAUD */
848 cfsetispeed(&termbuf
, val
);
849 #endif /* DECODE_BAUD */
855 * Open the slave side of the pty, and do any initialization
869 extern int def_row
, def_col
;
871 extern int def_tspeed
, def_rspeed
;
873 * Opening the slave side may cause initilization of the
874 * kernel tty structure. We need remember the state of
875 * if linemode was turned on
876 * terminal window size
879 * so that we can re-set them if we need to.
882 waslm
= tty_linemode();
884 erase
= termbuf
.c_cc
[VERASE
];
887 * Make sure that we don't have a controlling tty, and
888 * that we are the session (process group) leader.
891 t
= open(_PATH_TTY
, O_RDWR
);
893 (void) ioctl(t
, TIOCNOTTY
, NULL
);
900 fatalperror(net
, line
);
904 * set up the tty modes as we like them to be.
908 if (def_row
|| def_col
) {
909 memset((char *)&ws
, 0, sizeof(ws
));
912 (void)ioctl(t
, TIOCSWINSZ
, (char *)&ws
);
917 * Settings for sgtty based systems
920 termbuf
.sg
.sg_flags
|= CRMOD
|ANYP
|ECHO
|XTABS
;
921 # endif /* USE_TERMIO */
924 * Settings for all other termios/termio based
925 * systems, other than 4.4BSD. In 4.4BSD the
926 * kernel does the initial terminal setup.
928 tty_rspeed((def_rspeed
> 0) ? def_rspeed
: 9600);
929 tty_tspeed((def_tspeed
> 0) ? def_tspeed
: 9600);
931 termbuf
.c_cc
[VERASE
] = erase
;
935 # endif /* LINEMODE */
938 * Set the tty modes, and make this our controlling tty.
941 if (login_tty(t
) == -1)
942 fatalperror(net
, "login_tty");
945 #ifdef AUTHENTICATION
946 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
948 * Leave the pty open so that we can write out the rlogin
949 * protocol for /bin/login, if the authentication works.
957 #endif /* AUTHENTICATION */
964 * Open the specified slave side of the pty,
965 * making sure that we have a clean tty.
973 * Make sure that other people can't open the
974 * slave side of the connection.
976 (void) chown(li
, 0, 0);
977 (void) chmod(li
, 0600);
981 t
= open(line
, O_RDWR
|O_NOCTTY
);
992 * Given a hostname, do whatever
993 * is necessary to startup the login process on the slave side of the pty.
998 startslave(char *host
, int autologin
, char *autoname
)
1002 #ifdef AUTHENTICATION
1003 if (!autoname
|| !autoname
[0])
1006 if (autologin
< auth_level
) {
1007 fatal(net
, "Authorization failed");
1013 if ((i
= fork()) < 0)
1014 fatalperror(net
, "fork");
1018 start_login(host
, autologin
, autoname
);
1029 if ((*envp
= getenv("TZ")))
1039 * Assuming that we are now running as a child processes, this
1040 * function will turn us into the login process.
1043 #ifndef AUTHENTICATION
1044 #define undef1 __unused
1050 start_login(char *host undef1
, int autologin undef1
, char *name undef1
)
1057 * -h : pass on name of host.
1058 * WARNING: -h is accepted by login if and only if
1060 * -p : don't clobber the environment (so terminal type stays set).
1062 * -f : force this login, he has already been authenticated
1064 argv
= addarg(0, "login");
1066 #if !defined(NO_LOGIN_H)
1067 #ifdef AUTHENTICATION
1068 # if defined(NO_LOGIN_F) && defined(LOGIN_R)
1070 * Don't add the "-h host" option if we are going
1071 * to be adding the "-r host" option down below...
1073 if ((auth_level
< 0) || (autologin
!= AUTH_VALID
))
1076 argv
= addarg(argv
, "-h");
1077 argv
= addarg(argv
, host
);
1079 #endif /* AUTHENTICATION */
1081 #if !defined(NO_LOGIN_P)
1082 argv
= addarg(argv
, "-p");
1086 * Set the environment variable "LINEMODE" to either
1087 * "real" or "kludge" if we are operating in either
1088 * real or kludge linemode.
1090 if (lmodetype
== REAL_LINEMODE
) {
1091 if (setenv("LINEMODE", "real", 1) == -1)
1092 syslog(LOG_ERR
, "setenv: cannot set LINEMODE=real: %m");
1094 # ifdef KLUDGELINEMODE
1095 else if (lmodetype
== KLUDGE_LINEMODE
|| lmodetype
== KLUDGE_OK
) {
1096 if (setenv("LINEMODE", "kludge", 1) == -1)
1097 syslog(LOG_ERR
, "setenv: cannot set LINEMODE=kludge: %m");
1103 * Are we working as the bftp daemon? If so, then ask login
1104 * to start bftp instead of shell.
1107 argv
= addarg(argv
, "-e");
1108 argv
= addarg(argv
, BFTPPATH
);
1111 #ifdef AUTHENTICATION
1112 if (auth_level
>= 0 && autologin
== AUTH_VALID
) {
1113 # if !defined(NO_LOGIN_F)
1114 argv
= addarg(argv
, "-f");
1115 argv
= addarg(argv
, "--");
1116 argv
= addarg(argv
, name
);
1118 # if defined(LOGIN_R)
1120 * We don't have support for "login -f", but we
1121 * can fool /bin/login into thinking that we are
1122 * rlogind, and allow us to log in without a
1123 * password. The rlogin protocol expects
1124 * local-user\0remote-user\0term/speed\0
1130 int isecho
, israw
, xpty
, len
;
1131 extern int def_rspeed
;
1134 * Tell login that we are coming from "localhost".
1135 * If we passed in the real host name, then the
1136 * user would have to allow .rhost access from
1137 * every machine that they want authenticated
1138 * access to work from, which sort of defeats
1139 * the purpose of an authenticated login...
1140 * So, we tell login that the session is coming
1141 * from "localhost", and the user will only have
1142 * to have "localhost" in their .rhost file.
1144 # define LOGIN_HOST "localhost"
1146 argv
= addarg(argv
, "-r");
1147 argv
= addarg(argv
, LOGIN_HOST
);
1152 isecho
= tty_isecho();
1153 israw
= tty_israw();
1154 if (isecho
|| !israw
) {
1155 tty_setecho(0); /* Turn off echo */
1156 tty_setraw(1); /* Turn on raw */
1159 len
= strlen(name
)+1;
1160 write(xpty
, name
, len
);
1161 write(xpty
, name
, len
);
1162 snprintf(speed
, sizeof(speed
),
1163 "%s/%d", (cp
= getenv("TERM")) ? cp
: "",
1164 (def_rspeed
> 0) ? def_rspeed
: 9600);
1165 len
= strlen(speed
)+1;
1166 write(xpty
, speed
, len
);
1168 if (isecho
|| !israw
) {
1170 tty_setecho(isecho
);
1175 * Write a newline to ensure
1176 * that login will be able to
1179 write(xpty
, "\n", 1);
1185 argv
= addarg(argv
, "--");
1186 argv
= addarg(argv
, name
);
1191 if (getenv("USER")) {
1192 argv
= addarg(argv
, "--");
1193 argv
= addarg(argv
, getenv("USER"));
1194 #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1197 for (cpp
= environ
; *cpp
; cpp
++)
1198 argv
= addarg(argv
, *cpp
);
1202 * Assume that login will set the USER variable
1203 * correctly. For SysV systems, this means that
1204 * USER will no longer be set, just LOGNAME by
1205 * login. (The problem is that if the auto-login
1206 * fails, and the user then specifies a different
1207 * account name, he can get logged in with both
1208 * LOGNAME and USER in his environment, but the
1209 * USER value will be wrong.
1213 #ifdef AUTHENTICATION
1214 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
1218 #endif /* AUTHENTICATION */
1221 if (altlogin
== NULL
) {
1222 altlogin
= _PATH_LOGIN
;
1224 execv(altlogin
, argv
);
1226 syslog(LOG_ERR
, "%s: %m", altlogin
);
1227 fatalperror(net
, altlogin
);
1232 addarg(char **argv
, const char *val
)
1238 * 10 entries, a leading length, and a null
1240 argv
= (char **)malloc(sizeof(*argv
) * 12);
1243 *argv
++ = (char *)10;
1246 for (cpp
= argv
; *cpp
; cpp
++)
1248 if (cpp
== &argv
[(long)argv
[-1]]) {
1250 *argv
= (char *)((long)(*argv
) + 10);
1251 argv
= (char **)realloc(argv
, sizeof(*argv
)*((long)(*argv
) + 2));
1255 cpp
= &argv
[(long)argv
[-1] - 10];
1257 *cpp
++ = strdup(val
);
1265 * We only accept the environment variables listed below.
1270 static const char *rej
[] = {
1275 static const char *acc
[] = {
1276 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
1288 char ** new_environ
;
1291 /* Allocate space for scrubbed environment. */
1292 for (count
= 1, cpp
= environ
; *cpp
; count
++, cpp
++)
1294 if ((new_environ
= malloc(count
* sizeof(char *))) == NULL
) {
1299 for (cpp2
= new_environ
, cpp
= environ
; *cpp
; cpp
++) {
1302 for(p
= rej
; *p
; p
++)
1303 if(strncmp(*cpp
, *p
, strlen(*p
)) == 0) {
1310 for(p
= acc
; *p
; p
++)
1311 if(strncmp(*cpp
, *p
, strlen(*p
)) == 0)
1314 if ((*cpp2
++ = strdup(*cpp
)) == NULL
) {
1315 environ
= new_environ
;
1321 environ
= new_environ
;
1327 * This is the routine to call when we are all through, to
1328 * clean up anything that needs to be cleaned up.
1332 cleanup(int sig __unused
)
1337 p
= line
+ sizeof(_PATH_DEV
) - 1;
1339 * Block all signals before clearing the utmp entry. We don't want to
1340 * be called again after calling logout() and then not add the wtmp
1341 * entry because of not finding the corresponding entry in utmp.
1344 sigprocmask(SIG_SETMASK
, &mask
, NULL
);
1347 (void)chmod(line
, 0666);
1348 (void)chown(line
, 0, 0);
1350 (void)chmod(line
, 0666);
1351 (void)chown(line
, 0, 0);
1352 (void) shutdown(net
, SHUT_RDWR
);