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
36 static const char sccsid
[] = "@(#)sys_term.c 8.4+1 (Berkeley) 5/30/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/contrib/telnet/telnetd/sys_term.c,v 1.18 2003/05/04 02:54:49 obrien Exp $");
42 #include <sys/types.h>
45 #if (!defined(__BEOS__) && !defined(__HAIKU__))
51 #include "pathnames.h"
54 #include <libtelnet/auth.h>
57 int cleanopen(char *);
60 #if (!defined(__BEOS__) && !defined(__HAIKU__))
65 char wtmpf
[] = _PATH_WTMP
;
67 char wtmpf
[] = "/var/log/wtmp";
70 char utmpf
[] = _PATH_UTMP
;
72 char utmpf
[] = "/var/run/utmp";
77 extern char **environ
;
79 #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
80 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
107 # define cfsetospeed(tp, val) (tp)->sg.sg_ospeed = (val)
108 # define cfsetispeed(tp, val) (tp)->sg.sg_ispeed = (val)
109 # define cfgetospeed(tp) (tp)->sg.sg_ospeed
110 # define cfgetispeed(tp) (tp)->sg.sg_ispeed
111 #else /* USE_TERMIO */
114 # define TCSANOW TCSETS
115 # define TCSADRAIN TCSETSW
116 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
119 # define TCSANOW TCSETA
120 # define TCSADRAIN TCSETAW
121 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
123 # define TCSANOW TIOCSETA
124 # define TCSADRAIN TIOCSETAW
125 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
128 # define tcsetattr(f, a, t) ioctl(f, a, t)
129 # define cfsetospeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
130 (tp)->c_cflag |= (val)
131 # define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)
133 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CIBAUD; \
134 (tp)->c_cflag |= ((val)<<IBSHIFT)
135 # define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
137 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
138 (tp)->c_cflag |= (val)
139 # define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)
141 # endif /* TCSANOW */
142 struct termios termbuf
, termbuf2
; /* pty control structure */
143 #endif /* USE_TERMIO */
145 #include <sys/types.h>
148 int cleanopen(char *);
149 void scrub_env(void);
150 static char **addarg(char **, const char *);
157 * These three routines are used to get and set the "termbuf" structure
158 * to and from the kernel. init_termbuf() gets the current settings.
159 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
160 * set_termbuf() writes the structure into the kernel.
167 (void) ioctl(pty
, TIOCGETP
, (char *)&termbuf
.sg
);
168 (void) ioctl(pty
, TIOCGETC
, (char *)&termbuf
.tc
);
169 (void) ioctl(pty
, TIOCGLTC
, (char *)&termbuf
.ltc
);
171 (void) ioctl(pty
, TIOCGSTATE
, (char *)&termbuf
.state
);
174 (void) tcgetattr(pty
, &termbuf
);
179 #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
181 copy_termbuf(char *cp
, size_t len
)
183 if (len
> sizeof(termbuf
))
184 len
= sizeof(termbuf
);
185 memmove((char *)&termbuf
, cp
, len
);
188 #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
194 * Only make the necessary changes.
197 if (memcmp((char *)&termbuf
.sg
, (char *)&termbuf2
.sg
,
199 (void) ioctl(pty
, TIOCSETN
, (char *)&termbuf
.sg
);
200 if (memcmp((char *)&termbuf
.tc
, (char *)&termbuf2
.tc
,
202 (void) ioctl(pty
, TIOCSETC
, (char *)&termbuf
.tc
);
203 if (memcmp((char *)&termbuf
.ltc
, (char *)&termbuf2
.ltc
,
204 sizeof(termbuf
.ltc
)))
205 (void) ioctl(pty
, TIOCSLTC
, (char *)&termbuf
.ltc
);
206 if (termbuf
.lflags
!= termbuf2
.lflags
)
207 (void) ioctl(pty
, TIOCLSET
, (char *)&termbuf
.lflags
);
208 #else /* USE_TERMIO */
209 if (memcmp((char *)&termbuf
, (char *)&termbuf2
, sizeof(termbuf
)))
210 (void) tcsetattr(pty
, TCSANOW
, &termbuf
);
211 #endif /* USE_TERMIO */
216 * spcset(func, valp, valpp)
218 * This function takes various special characters (func), and
219 * sets *valp to the current value of that character, and
220 * *valpp to point to where in the "termbuf" structure that
223 * It returns the SLC_ level of support for this function.
228 spcset(int func
, cc_t
*valp
, cc_t
**valpp
)
232 *valp
= termbuf
.tc
.t_eofc
;
233 *valpp
= (cc_t
*)&termbuf
.tc
.t_eofc
;
234 return(SLC_VARIABLE
);
236 *valp
= termbuf
.sg
.sg_erase
;
237 *valpp
= (cc_t
*)&termbuf
.sg
.sg_erase
;
238 return(SLC_VARIABLE
);
240 *valp
= termbuf
.sg
.sg_kill
;
241 *valpp
= (cc_t
*)&termbuf
.sg
.sg_kill
;
242 return(SLC_VARIABLE
);
244 *valp
= termbuf
.tc
.t_intrc
;
245 *valpp
= (cc_t
*)&termbuf
.tc
.t_intrc
;
246 return(SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
248 *valp
= termbuf
.tc
.t_quitc
;
249 *valpp
= (cc_t
*)&termbuf
.tc
.t_quitc
;
250 return(SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
252 *valp
= termbuf
.tc
.t_startc
;
253 *valpp
= (cc_t
*)&termbuf
.tc
.t_startc
;
254 return(SLC_VARIABLE
);
256 *valp
= termbuf
.tc
.t_stopc
;
257 *valpp
= (cc_t
*)&termbuf
.tc
.t_stopc
;
258 return(SLC_VARIABLE
);
260 *valp
= termbuf
.ltc
.t_flushc
;
261 *valpp
= (cc_t
*)&termbuf
.ltc
.t_flushc
;
262 return(SLC_VARIABLE
);
264 *valp
= termbuf
.ltc
.t_suspc
;
265 *valpp
= (cc_t
*)&termbuf
.ltc
.t_suspc
;
266 return(SLC_VARIABLE
);
268 *valp
= termbuf
.ltc
.t_werasc
;
269 *valpp
= (cc_t
*)&termbuf
.ltc
.t_werasc
;
270 return(SLC_VARIABLE
);
272 *valp
= termbuf
.ltc
.t_rprntc
;
273 *valpp
= (cc_t
*)&termbuf
.ltc
.t_rprntc
;
274 return(SLC_VARIABLE
);
276 *valp
= termbuf
.ltc
.t_lnextc
;
277 *valpp
= (cc_t
*)&termbuf
.ltc
.t_lnextc
;
278 return(SLC_VARIABLE
);
280 *valp
= termbuf
.tc
.t_brkc
;
281 *valpp
= (cc_t
*)&termbuf
.ltc
.t_lnextc
;
282 return(SLC_VARIABLE
);
293 return(SLC_NOSUPPORT
);
297 #else /* USE_TERMIO */
300 #define setval(a, b) *valp = termbuf.c_cc[a]; \
301 *valpp = &termbuf.c_cc[a]; \
303 #define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT);
306 spcset(int func
, cc_t
*valp
, cc_t
**valpp
)
310 setval(VEOF
, SLC_VARIABLE
);
312 setval(VERASE
, SLC_VARIABLE
);
314 setval(VKILL
, SLC_VARIABLE
);
316 setval(VINTR
, SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
318 setval(VQUIT
, SLC_VARIABLE
|SLC_FLUSHIN
|SLC_FLUSHOUT
);
321 setval(VSTART
, SLC_VARIABLE
);
327 setval(VSTOP
, SLC_VARIABLE
);
333 setval(VWERASE
, SLC_VARIABLE
);
339 setval(VREPRINT
, SLC_VARIABLE
);
345 setval(VLNEXT
, SLC_VARIABLE
);
350 #if !defined(VDISCARD) && defined(VFLUSHO)
351 # define VDISCARD VFLUSHO
354 setval(VDISCARD
, SLC_VARIABLE
|SLC_FLUSHOUT
);
360 setval(VSUSP
, SLC_VARIABLE
|SLC_FLUSHIN
);
366 setval(VEOL
, SLC_VARIABLE
);
370 setval(VEOL2
, SLC_VARIABLE
);
374 setval(VSTATUS
, SLC_VARIABLE
);
387 return(SLC_NOSUPPORT
);
390 #endif /* USE_TERMIO */
395 * Allocate a pty. As a side effect, the external character
396 * array "line" contains the name of the slave side.
398 * Returns the file descriptor of the opened pty.
400 char alpha
[] = "0123456789abcdefghijklmnopqrstuv";
404 getpty(int *ptynum __unused
)
411 #if (defined(__BEOS__) || defined(__HAIKU__))
412 (void) strcpy(line
, "/dev/pt/XX");
414 (void) strcpy(line
, _PATH_DEV
);
415 (void) strcat(line
, "ptyXX");
420 for (cp
= "pqrsPQRS"; *cp
; cp
++) {
426 * This stat() check is just to keep us from
427 * looping through all 256 combinations if there
428 * aren't that many ptys available.
430 if (stat(line
, &stb
) < 0)
432 for (i
= 0; i
< 32; i
++) {
448 * tty_flowmode() Find out if flow control is enabled or disabled.
449 * tty_linemode() Find out if linemode (external processing) is enabled.
450 * tty_setlinemod(on) Turn on/off linemode.
451 * tty_isecho() Find out if echoing is turned on.
452 * tty_setecho(on) Enable/disable character echoing.
453 * tty_israw() Find out if terminal is in RAW mode.
454 * tty_binaryin(on) Turn on/off BINARY on input.
455 * tty_binaryout(on) Turn on/off BINARY on output.
456 * tty_isediting() Find out if line editing is enabled.
457 * tty_istrapsig() Find out if signal trapping is enabled.
458 * tty_setedit(on) Turn on/off line editing.
459 * tty_setsig(on) Turn on/off signal trapping.
460 * tty_issofttab() Find out if tab expansion is enabled.
461 * tty_setsofttab(on) Turn on/off soft tab expansion.
462 * tty_islitecho() Find out if typed control chars are echoed literally
463 * tty_setlitecho() Turn on/off literal echo of control chars
464 * tty_tspeed(val) Set transmit speed to val.
465 * tty_rspeed(val) Set receive speed to val.
473 return(termbuf
.state
& TS_EXTPROC
);
475 return(termbuf
.c_lflag
& EXTPROC
);
480 tty_setlinemode(int on
)
484 (void) ioctl(pty
, TIOCEXT
, (char *)&on
);
489 termbuf
.c_lflag
|= EXTPROC
;
491 termbuf
.c_lflag
&= ~EXTPROC
;
495 #endif /* LINEMODE */
501 return (termbuf
.sg
.sg_flags
& ECHO
);
503 return (termbuf
.c_lflag
& ECHO
);
511 return(((termbuf
.tc
.t_startc
) > 0 && (termbuf
.tc
.t_stopc
) > 0) ? 1 : 0);
513 return((termbuf
.c_iflag
& IXON
) ? 1 : 0);
522 return((termbuf
.lflags
& DECCTQ
) ? 0 : 1);
527 return((termbuf
.c_iflag
& IXANY
) ? 1 : 0);
536 termbuf
.sg
.sg_flags
|= ECHO
|CRMOD
;
538 termbuf
.sg
.sg_flags
&= ~(ECHO
|CRMOD
);
541 termbuf
.c_lflag
|= ECHO
;
543 termbuf
.c_lflag
&= ~ECHO
;
551 return(termbuf
.sg
.sg_flags
& RAW
);
553 return(!(termbuf
.c_lflag
& ICANON
));
557 #ifdef AUTHENTICATION
558 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
564 termbuf
.sg
.sg_flags
|= RAW
;
566 termbuf
.sg
.sg_flags
&= ~RAW
;
569 termbuf
.c_lflag
&= ~ICANON
;
571 termbuf
.c_lflag
|= ICANON
;
575 #endif /* AUTHENTICATION */
582 termbuf
.lflags
|= LPASS8
;
584 termbuf
.lflags
&= ~LPASS8
;
587 termbuf
.c_iflag
&= ~ISTRIP
;
589 termbuf
.c_iflag
|= ISTRIP
;
595 tty_binaryout(int on
)
599 termbuf
.lflags
|= LLITOUT
;
601 termbuf
.lflags
&= ~LLITOUT
;
604 termbuf
.c_cflag
&= ~(CSIZE
|PARENB
);
605 termbuf
.c_cflag
|= CS8
;
606 termbuf
.c_oflag
&= ~OPOST
;
608 termbuf
.c_cflag
&= ~CSIZE
;
609 termbuf
.c_cflag
|= CS7
|PARENB
;
610 termbuf
.c_oflag
|= OPOST
;
619 return(termbuf
.lflags
& LPASS8
);
621 return(!(termbuf
.c_iflag
& ISTRIP
));
626 tty_isbinaryout(void)
629 return(termbuf
.lflags
& LLITOUT
);
631 return(!(termbuf
.c_oflag
&OPOST
));
640 return(!(termbuf
.sg
.sg_flags
& (CBREAK
|RAW
)));
642 return(termbuf
.c_lflag
& ICANON
);
650 return(!(termbuf
.sg
.sg_flags
&RAW
));
652 return(termbuf
.c_lflag
& ISIG
);
661 termbuf
.sg
.sg_flags
&= ~CBREAK
;
663 termbuf
.sg
.sg_flags
|= CBREAK
;
666 termbuf
.c_lflag
|= ICANON
;
668 termbuf
.c_lflag
&= ~ICANON
;
680 termbuf
.c_lflag
|= ISIG
;
682 termbuf
.c_lflag
&= ~ISIG
;
685 #endif /* LINEMODE */
691 return (termbuf
.sg
.sg_flags
& XTABS
);
694 return (termbuf
.c_oflag
& OXTABS
);
697 return ((termbuf
.c_oflag
& TABDLY
) == TAB3
);
703 tty_setsofttab(int on
)
707 termbuf
.sg
.sg_flags
|= XTABS
;
709 termbuf
.sg
.sg_flags
&= ~XTABS
;
713 termbuf
.c_oflag
|= OXTABS
;
716 termbuf
.c_oflag
&= ~TABDLY
;
717 termbuf
.c_oflag
|= TAB3
;
721 termbuf
.c_oflag
&= ~OXTABS
;
724 termbuf
.c_oflag
&= ~TABDLY
;
725 termbuf
.c_oflag
|= TAB0
;
735 return (!(termbuf
.lflags
& LCTLECH
));
738 return (!(termbuf
.c_lflag
& ECHOCTL
));
741 return (!(termbuf
.c_lflag
& TCTLECH
));
743 # if !defined(ECHOCTL) && !defined(TCTLECH)
744 return (0); /* assumes ctl chars are echoed '^x' */
750 tty_setlitecho(int on
)
754 termbuf
.lflags
&= ~LCTLECH
;
756 termbuf
.lflags
|= LCTLECH
;
760 termbuf
.c_lflag
&= ~ECHOCTL
;
762 termbuf
.c_lflag
|= ECHOCTL
;
766 termbuf
.c_lflag
&= ~TCTLECH
;
768 termbuf
.c_lflag
|= TCTLECH
;
777 return (termbuf
.sg
.sg_flags
& CRMOD
);
779 return (termbuf
.c_iflag
& ICRNL
);
784 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
793 * A table of available terminal speeds
799 { 0, B0
}, { 50, B50
}, { 75, B75
},
800 { 110, B110
}, { 134, B134
}, { 150, B150
},
801 { 200, B200
}, { 300, B300
}, { 600, B600
},
802 { 1200, B1200
}, { 1800, B1800
}, { 2400, B2400
},
831 #endif /* DECODE_BAUD */
837 struct termspeeds
*tp
;
839 for (tp
= termspeeds
; (tp
->speed
!= -1) && (val
> tp
->speed
); tp
++)
841 if (tp
->speed
== -1) /* back up to last valid value */
843 cfsetospeed(&termbuf
, tp
->value
);
844 #else /* DECODE_BAUD */
845 cfsetospeed(&termbuf
, val
);
846 #endif /* DECODE_BAUD */
853 struct termspeeds
*tp
;
855 for (tp
= termspeeds
; (tp
->speed
!= -1) && (val
> tp
->speed
); tp
++)
857 if (tp
->speed
== -1) /* back up to last valid value */
859 cfsetispeed(&termbuf
, tp
->value
);
860 #else /* DECODE_BAUD */
861 cfsetispeed(&termbuf
, val
);
862 #endif /* DECODE_BAUD */
868 * Open the slave side of the pty, and do any initialization
882 extern int def_row
, def_col
;
884 extern int def_tspeed
, def_rspeed
;
886 * Opening the slave side may cause initilization of the
887 * kernel tty structure. We need remember the state of
888 * if linemode was turned on
889 * terminal window size
892 * so that we can re-set them if we need to.
895 waslm
= tty_linemode();
897 erase
= termbuf
.c_cc
[VERASE
];
900 * Make sure that we don't have a controlling tty, and
901 * that we are the session (process group) leader.
904 t
= open(_PATH_TTY
, O_RDWR
);
906 (void) ioctl(t
, TIOCNOTTY
, (char *)0);
913 fatalperror(net
, line
);
917 * set up the tty modes as we like them to be.
921 if (def_row
|| def_col
) {
922 memset((char *)&ws
, 0, sizeof(ws
));
925 (void)ioctl(t
, TIOCSWINSZ
, (char *)&ws
);
930 * Settings for sgtty based systems
933 termbuf
.sg
.sg_flags
|= CRMOD
|ANYP
|ECHO
|XTABS
;
934 # endif /* USE_TERMIO */
937 * Settings for all other termios/termio based
938 * systems, other than 4.4BSD. In 4.4BSD the
939 * kernel does the initial terminal setup.
941 tty_rspeed((def_rspeed
> 0) ? def_rspeed
: 9600);
942 tty_tspeed((def_tspeed
> 0) ? def_tspeed
: 9600);
944 termbuf
.c_cc
[VERASE
] = erase
;
948 # endif /* LINEMODE */
951 * Set the tty modes, and make this our controlling tty.
954 if (login_tty(t
) == -1)
955 fatalperror(net
, "login_tty");
958 #ifdef AUTHENTICATION
959 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
961 * Leave the pty open so that we can write out the rlogin
962 * protocol for /bin/login, if the authentication works.
970 #endif /* AUTHENTICATION */
977 * Open the specified slave side of the pty,
978 * making sure that we have a clean tty.
986 * Make sure that other people can't open the
987 * slave side of the connection.
989 (void) chown(li
, 0, 0);
990 (void) chmod(li
, 0600);
992 #if (!defined(__BEOS__) && !defined(__HAIKU__))
996 t
= open(line
, O_RDWR
|O_NOCTTY
);
1005 #if (defined(__BEOS__) || defined(__HAIKU__))
1006 /* taken from DragonFly's telnetd */
1011 fatalperror(net
, "setsid()");
1015 * We get our controlling tty assigned as a side-effect
1016 * of opening up a tty device. But on BSD based systems,
1017 * this only happens if our process group is zero. The
1018 * setsid() call above may have set our pgrp, so clear
1019 * it out before opening the tty...
1022 close(open(line
, O_RDWR
));
1024 setenv("TTY", line
, 1);
1036 #endif /* __BEOS__ */
1041 * Given a hostname, do whatever
1042 * is necessary to startup the login process on the slave side of the pty.
1047 startslave(char *host
, int autologin
, char *autoname
)
1051 #ifdef AUTHENTICATION
1052 if (!autoname
|| !autoname
[0])
1055 if (autologin
< auth_level
) {
1056 fatal(net
, "Authorization failed");
1062 if ((i
= fork()) < 0)
1063 fatalperror(net
, "fork");
1067 start_login(host
, autologin
, autoname
);
1078 if ((*envp
= getenv("TZ")))
1088 * Assuming that we are now running as a child processes, this
1089 * function will turn us into the login process.
1092 #ifndef AUTHENTICATION
1093 #define undef1 __unused
1099 start_login(char *host undef1
, int autologin undef1
, char *name undef1
)
1106 * -h : pass on name of host.
1107 * WARNING: -h is accepted by login if and only if
1109 * -p : don't clobber the environment (so terminal type stays set).
1111 * -f : force this login, he has already been authenticated
1113 argv
= addarg(0, "login");
1115 #if !defined(NO_LOGIN_H)
1116 #ifdef AUTHENTICATION
1117 # if defined(NO_LOGIN_F) && defined(LOGIN_R)
1119 * Don't add the "-h host" option if we are going
1120 * to be adding the "-r host" option down below...
1122 if ((auth_level
< 0) || (autologin
!= AUTH_VALID
))
1125 argv
= addarg(argv
, "-h");
1126 argv
= addarg(argv
, host
);
1128 #endif /* AUTHENTICATION */
1130 #if !defined(NO_LOGIN_P)
1131 argv
= addarg(argv
, "-p");
1135 * Set the environment variable "LINEMODE" to either
1136 * "real" or "kludge" if we are operating in either
1137 * real or kludge linemode.
1139 if (lmodetype
== REAL_LINEMODE
)
1140 setenv("LINEMODE", "real", 1);
1141 # ifdef KLUDGELINEMODE
1142 else if (lmodetype
== KLUDGE_LINEMODE
|| lmodetype
== KLUDGE_OK
)
1143 setenv("LINEMODE", "kludge", 1);
1148 * Are we working as the bftp daemon? If so, then ask login
1149 * to start bftp instead of shell.
1152 argv
= addarg(argv
, "-e");
1153 argv
= addarg(argv
, BFTPPATH
);
1156 #ifdef AUTHENTICATION
1157 if (auth_level
>= 0 && autologin
== AUTH_VALID
) {
1158 # if !defined(NO_LOGIN_F)
1159 argv
= addarg(argv
, "-f");
1160 argv
= addarg(argv
, "--");
1161 argv
= addarg(argv
, name
);
1163 # if defined(LOGIN_R)
1165 * We don't have support for "login -f", but we
1166 * can fool /bin/login into thinking that we are
1167 * rlogind, and allow us to log in without a
1168 * password. The rlogin protocol expects
1169 * local-user\0remote-user\0term/speed\0
1175 int isecho
, israw
, xpty
, len
;
1176 extern int def_rspeed
;
1179 * Tell login that we are coming from "localhost".
1180 * If we passed in the real host name, then the
1181 * user would have to allow .rhost access from
1182 * every machine that they want authenticated
1183 * access to work from, which sort of defeats
1184 * the purpose of an authenticated login...
1185 * So, we tell login that the session is coming
1186 * from "localhost", and the user will only have
1187 * to have "localhost" in their .rhost file.
1189 # define LOGIN_HOST "localhost"
1191 argv
= addarg(argv
, "-r");
1192 argv
= addarg(argv
, LOGIN_HOST
);
1197 isecho
= tty_isecho();
1198 israw
= tty_israw();
1199 if (isecho
|| !israw
) {
1200 tty_setecho(0); /* Turn off echo */
1201 tty_setraw(1); /* Turn on raw */
1204 len
= strlen(name
)+1;
1205 write(xpty
, name
, len
);
1206 write(xpty
, name
, len
);
1207 snprintf(speed
, sizeof(speed
),
1208 "%s/%d", (cp
= getenv("TERM")) ? cp
: "",
1209 (def_rspeed
> 0) ? def_rspeed
: 9600);
1210 len
= strlen(speed
)+1;
1211 write(xpty
, speed
, len
);
1213 if (isecho
|| !israw
) {
1215 tty_setecho(isecho
);
1220 * Write a newline to ensure
1221 * that login will be able to
1224 write(xpty
, "\n", 1);
1230 argv
= addarg(argv
, "--");
1231 argv
= addarg(argv
, name
);
1236 if (getenv("USER")) {
1237 argv
= addarg(argv
, "--");
1238 argv
= addarg(argv
, getenv("USER"));
1239 #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1242 for (cpp
= environ
; *cpp
; cpp
++)
1243 argv
= addarg(argv
, *cpp
);
1247 * Assume that login will set the USER variable
1248 * correctly. For SysV systems, this means that
1249 * USER will no longer be set, just LOGNAME by
1250 * login. (The problem is that if the auto-login
1251 * fails, and the user then specifies a different
1252 * account name, he can get logged in with both
1253 * LOGNAME and USER in his environment, but the
1254 * USER value will be wrong.
1258 #ifdef AUTHENTICATION
1259 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
1263 #endif /* AUTHENTICATION */
1266 if (altlogin
== NULL
) {
1267 altlogin
= _PATH_LOGIN
;
1269 execv(altlogin
, argv
);
1271 syslog(LOG_ERR
, "%s: %m", altlogin
);
1272 fatalperror(net
, altlogin
);
1277 addarg(char **argv
, const char *val
)
1283 * 10 entries, a leading length, and a null
1285 argv
= (char **)malloc(sizeof(*argv
) * 12);
1288 *argv
++ = (char *)10;
1291 for (cpp
= argv
; *cpp
; cpp
++)
1293 if (cpp
== &argv
[(long)argv
[-1]]) {
1295 *argv
= (char *)((long)(*argv
) + 10);
1296 argv
= (char **)realloc(argv
, sizeof(*argv
)*((long)(*argv
) + 2));
1300 cpp
= &argv
[(long)argv
[-1] - 10];
1302 *cpp
++ = strdup(val
);
1310 * We only accept the environment variables listed below.
1315 static const char *rej
[] = {
1320 static const char *acc
[] = {
1321 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
1334 for (cpp2
= cpp
= environ
; *cpp
; cpp
++) {
1337 for(p
= rej
; *p
; p
++)
1338 if(strncmp(*cpp
, *p
, strlen(*p
)) == 0) {
1345 for(p
= acc
; *p
; p
++)
1346 if(strncmp(*cpp
, *p
, strlen(*p
)) == 0)
1357 * This is the routine to call when we are all through, to
1358 * clean up anything that needs to be cleaned up.
1362 cleanup(int sig __unused
)
1367 p
= line
+ sizeof(_PATH_DEV
) - 1;
1369 * Block all signals before clearing the utmp entry. We don't want to
1370 * be called again after calling logout() and then not add the wtmp
1371 * entry because of not finding the corresponding entry in utmp.
1374 sigprocmask(SIG_SETMASK
, &mask
, NULL
);
1375 #if (!defined(__BEOS__) && !defined(__HAIKU__))
1378 (void)chmod(line
, 0666);
1379 (void)chown(line
, 0, 0);
1381 (void)chmod(line
, 0666);
1382 (void)chown(line
, 0, 0);
1384 (void) shutdown(net
, 2);