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 #include <sys/types.h>
138 int cleanopen(char *);
139 void scrub_env(void);
140 static char **addarg(char **, const char *);
147 * These three routines are used to get and set the "termbuf" structure
148 * to and from the kernel. init_termbuf() gets the current settings.
149 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
150 * set_termbuf() writes the structure into the kernel.
157 (void) ioctl(pty, TIOCGETP, (char *)&termbuf.sg);
158 (void) ioctl(pty, TIOCGETC, (char *)&termbuf.tc);
159 (void) ioctl(pty, TIOCGLTC, (char *)&termbuf.ltc);
161 (void) ioctl(pty, TIOCGSTATE, (char *)&termbuf.state);
164 (void) tcgetattr(pty, &termbuf);
169 #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
171 copy_termbuf(char *cp, size_t len)
173 if (len > sizeof(termbuf))
174 len = sizeof(termbuf);
175 memmove((char *)&termbuf, cp, len);
178 #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
184 * Only make the necessary changes.
187 if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg,
189 (void) ioctl(pty, TIOCSETN, (char *)&termbuf.sg);
190 if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc,
192 (void) ioctl(pty, TIOCSETC, (char *)&termbuf.tc);
193 if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc,
194 sizeof(termbuf.ltc)))
195 (void) ioctl(pty, TIOCSLTC, (char *)&termbuf.ltc);
196 if (termbuf.lflags != termbuf2.lflags)
197 (void) ioctl(pty, TIOCLSET, (char *)&termbuf.lflags);
198 #else /* USE_TERMIO */
199 if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
200 (void) tcsetattr(pty, TCSANOW, &termbuf);
201 #endif /* USE_TERMIO */
206 * spcset(func, valp, valpp)
208 * This function takes various special characters (func), and
209 * sets *valp to the current value of that character, and
210 * *valpp to point to where in the "termbuf" structure that
213 * It returns the SLC_ level of support for this function.
218 spcset(int func, cc_t *valp, cc_t **valpp)
222 *valp = termbuf.tc.t_eofc;
223 *valpp = (cc_t *)&termbuf.tc.t_eofc;
224 return(SLC_VARIABLE);
226 *valp = termbuf.sg.sg_erase;
227 *valpp = (cc_t *)&termbuf.sg.sg_erase;
228 return(SLC_VARIABLE);
230 *valp = termbuf.sg.sg_kill;
231 *valpp = (cc_t *)&termbuf.sg.sg_kill;
232 return(SLC_VARIABLE);
234 *valp = termbuf.tc.t_intrc;
235 *valpp = (cc_t *)&termbuf.tc.t_intrc;
236 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
238 *valp = termbuf.tc.t_quitc;
239 *valpp = (cc_t *)&termbuf.tc.t_quitc;
240 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
242 *valp = termbuf.tc.t_startc;
243 *valpp = (cc_t *)&termbuf.tc.t_startc;
244 return(SLC_VARIABLE);
246 *valp = termbuf.tc.t_stopc;
247 *valpp = (cc_t *)&termbuf.tc.t_stopc;
248 return(SLC_VARIABLE);
250 *valp = termbuf.ltc.t_flushc;
251 *valpp = (cc_t *)&termbuf.ltc.t_flushc;
252 return(SLC_VARIABLE);
254 *valp = termbuf.ltc.t_suspc;
255 *valpp = (cc_t *)&termbuf.ltc.t_suspc;
256 return(SLC_VARIABLE);
258 *valp = termbuf.ltc.t_werasc;
259 *valpp = (cc_t *)&termbuf.ltc.t_werasc;
260 return(SLC_VARIABLE);
262 *valp = termbuf.ltc.t_rprntc;
263 *valpp = (cc_t *)&termbuf.ltc.t_rprntc;
264 return(SLC_VARIABLE);
266 *valp = termbuf.ltc.t_lnextc;
267 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
268 return(SLC_VARIABLE);
270 *valp = termbuf.tc.t_brkc;
271 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
272 return(SLC_VARIABLE);
283 return(SLC_NOSUPPORT);
287 #else /* USE_TERMIO */
290 #define setval(a, b) *valp = termbuf.c_cc[a]; \
291 *valpp = &termbuf.c_cc[a]; \
293 #define defval(a) *valp = ((cc_t)a); *valpp = NULL; return(SLC_DEFAULT);
296 spcset(int func, cc_t *valp, cc_t **valpp)
300 setval(VEOF, SLC_VARIABLE);
302 setval(VERASE, SLC_VARIABLE);
304 setval(VKILL, SLC_VARIABLE);
306 setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
308 setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
311 setval(VSTART, SLC_VARIABLE);
317 setval(VSTOP, SLC_VARIABLE);
323 setval(VWERASE, SLC_VARIABLE);
329 setval(VREPRINT, SLC_VARIABLE);
335 setval(VLNEXT, SLC_VARIABLE);
340 #if !defined(VDISCARD) && defined(VFLUSHO)
341 # define VDISCARD VFLUSHO
344 setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
350 setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
356 setval(VEOL, SLC_VARIABLE);
360 setval(VEOL2, SLC_VARIABLE);
364 setval(VSTATUS, SLC_VARIABLE);
377 return(SLC_NOSUPPORT);
380 #endif /* USE_TERMIO */
385 * Allocate a pty. As a side effect, the external character
386 * array "line" contains the name of the slave side.
388 * Returns the file descriptor of the opened pty.
390 char alpha[] = "0123456789abcdefghijklmnopqrstuv";
394 getpty(int *ptynum __unused)
401 (void) strcpy(line, _PATH_DEV);
402 (void) strcat(line, "ptyXX");
406 for (cp = "pqrsPQRS"; *cp; cp++) {
412 * This stat() check is just to keep us from
413 * looping through all 256 combinations if there
414 * aren't that many ptys available.
416 if (stat(line, &stb) < 0)
418 for (i = 0; i < 32; i++) {
434 * tty_flowmode() Find out if flow control is enabled or disabled.
435 * tty_linemode() Find out if linemode (external processing) is enabled.
436 * tty_setlinemod(on) Turn on/off linemode.
437 * tty_isecho() Find out if echoing is turned on.
438 * tty_setecho(on) Enable/disable character echoing.
439 * tty_israw() Find out if terminal is in RAW mode.
440 * tty_binaryin(on) Turn on/off BINARY on input.
441 * tty_binaryout(on) Turn on/off BINARY on output.
442 * tty_isediting() Find out if line editing is enabled.
443 * tty_istrapsig() Find out if signal trapping is enabled.
444 * tty_setedit(on) Turn on/off line editing.
445 * tty_setsig(on) Turn on/off signal trapping.
446 * tty_issofttab() Find out if tab expansion is enabled.
447 * tty_setsofttab(on) Turn on/off soft tab expansion.
448 * tty_islitecho() Find out if typed control chars are echoed literally
449 * tty_setlitecho() Turn on/off literal echo of control chars
450 * tty_tspeed(val) Set transmit speed to val.
451 * tty_rspeed(val) Set receive speed to val.
459 return(termbuf.state & TS_EXTPROC);
461 return(termbuf.c_lflag & EXTPROC);
466 tty_setlinemode(int on)
470 (void) ioctl(pty, TIOCEXT, (char *)&on);
475 termbuf.c_lflag |= EXTPROC;
477 termbuf.c_lflag &= ~EXTPROC;
481 #endif /* LINEMODE */
487 return (termbuf.sg.sg_flags & ECHO);
489 return (termbuf.c_lflag & ECHO);
497 return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0);
499 return((termbuf.c_iflag & IXON) ? 1 : 0);
508 return((termbuf.lflags & DECCTQ) ? 0 : 1);
513 return((termbuf.c_iflag & IXANY) ? 1 : 0);
522 termbuf.sg.sg_flags |= ECHO|CRMOD;
524 termbuf.sg.sg_flags &= ~(ECHO|CRMOD);
527 termbuf.c_lflag |= ECHO;
529 termbuf.c_lflag &= ~ECHO;
537 return(termbuf.sg.sg_flags & RAW);
539 return(!(termbuf.c_lflag & ICANON));
543 #ifdef AUTHENTICATION
544 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
550 termbuf.sg.sg_flags |= RAW;
552 termbuf.sg.sg_flags &= ~RAW;
555 termbuf.c_lflag &= ~ICANON;
557 termbuf.c_lflag |= ICANON;
561 #endif /* AUTHENTICATION */
568 termbuf.lflags |= LPASS8;
570 termbuf.lflags &= ~LPASS8;
573 termbuf.c_iflag &= ~ISTRIP;
575 termbuf.c_iflag |= ISTRIP;
581 tty_binaryout(int on)
585 termbuf.lflags |= LLITOUT;
587 termbuf.lflags &= ~LLITOUT;
590 termbuf.c_cflag &= ~(CSIZE|PARENB);
591 termbuf.c_cflag |= CS8;
592 termbuf.c_oflag &= ~OPOST;
594 termbuf.c_cflag &= ~CSIZE;
595 termbuf.c_cflag |= CS7|PARENB;
596 termbuf.c_oflag |= OPOST;
605 return(termbuf.lflags & LPASS8);
607 return(!(termbuf.c_iflag & ISTRIP));
612 tty_isbinaryout(void)
615 return(termbuf.lflags & LLITOUT);
617 return(!(termbuf.c_oflag&OPOST));
626 return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));
628 return(termbuf.c_lflag & ICANON);
636 return(!(termbuf.sg.sg_flags&RAW));
638 return(termbuf.c_lflag & ISIG);
647 termbuf.sg.sg_flags &= ~CBREAK;
649 termbuf.sg.sg_flags |= CBREAK;
652 termbuf.c_lflag |= ICANON;
654 termbuf.c_lflag &= ~ICANON;
666 termbuf.c_lflag |= ISIG;
668 termbuf.c_lflag &= ~ISIG;
671 #endif /* LINEMODE */
677 return (termbuf.sg.sg_flags & XTABS);
680 return (termbuf.c_oflag & OXTABS);
683 return ((termbuf.c_oflag & TABDLY) == TAB3);
689 tty_setsofttab(int on)
693 termbuf.sg.sg_flags |= XTABS;
695 termbuf.sg.sg_flags &= ~XTABS;
699 termbuf.c_oflag |= OXTABS;
702 termbuf.c_oflag &= ~TABDLY;
703 termbuf.c_oflag |= TAB3;
707 termbuf.c_oflag &= ~OXTABS;
710 termbuf.c_oflag &= ~TABDLY;
711 termbuf.c_oflag |= TAB0;
721 return (!(termbuf.lflags & LCTLECH));
724 return (!(termbuf.c_lflag & ECHOCTL));
727 return (!(termbuf.c_lflag & TCTLECH));
729 # if !defined(ECHOCTL) && !defined(TCTLECH)
730 return (0); /* assumes ctl chars are echoed '^x' */
736 tty_setlitecho(int on)
740 termbuf.lflags &= ~LCTLECH;
742 termbuf.lflags |= LCTLECH;
746 termbuf.c_lflag &= ~ECHOCTL;
748 termbuf.c_lflag |= ECHOCTL;
752 termbuf.c_lflag &= ~TCTLECH;
754 termbuf.c_lflag |= TCTLECH;
763 return (termbuf.sg.sg_flags & CRMOD);
765 return (termbuf.c_iflag & ICRNL);
770 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
779 * A table of available terminal speeds
785 { 0, B0 }, { 50, B50 }, { 75, B75 },
786 { 110, B110 }, { 134, B134 }, { 150, B150 },
787 { 200, B200 }, { 300, B300 }, { 600, B600 },
788 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
817 #endif /* DECODE_BAUD */
823 struct termspeeds *tp;
825 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
827 if (tp->speed == -1) /* back up to last valid value */
829 cfsetospeed(&termbuf, tp->value);
830 #else /* DECODE_BAUD */
831 cfsetospeed(&termbuf, val);
832 #endif /* DECODE_BAUD */
839 struct termspeeds *tp;
841 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
843 if (tp->speed == -1) /* back up to last valid value */
845 cfsetispeed(&termbuf, tp->value);
846 #else /* DECODE_BAUD */
847 cfsetispeed(&termbuf, val);
848 #endif /* DECODE_BAUD */
854 * Open the slave side of the pty, and do any initialization
868 extern int def_row, def_col;
870 extern int def_tspeed, def_rspeed;
872 * Opening the slave side may cause initilization of the
873 * kernel tty structure. We need remember the state of
874 * if linemode was turned on
875 * terminal window size
878 * so that we can re-set them if we need to.
881 waslm = tty_linemode();
883 erase = termbuf.c_cc[VERASE];
886 * Make sure that we don't have a controlling tty, and
887 * that we are the session (process group) leader.
890 t = open(_PATH_TTY, O_RDWR);
892 (void) ioctl(t, TIOCNOTTY, NULL);
899 fatalperror(net, line);
903 * set up the tty modes as we like them to be.
907 if (def_row || def_col) {
908 memset((char *)&ws, 0, sizeof(ws));
911 (void)ioctl(t, TIOCSWINSZ, (char *)&ws);
916 * Settings for sgtty based systems
919 termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
920 # endif /* USE_TERMIO */
923 * Settings for all other termios/termio based
924 * systems, other than 4.4BSD. In 4.4BSD the
925 * kernel does the initial terminal setup.
927 tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
928 tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
930 termbuf.c_cc[VERASE] = erase;
934 # endif /* LINEMODE */
937 * Set the tty modes, and make this our controlling tty.
940 if (login_tty(t) == -1)
941 fatalperror(net, "login_tty");
944 #ifdef AUTHENTICATION
945 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
947 * Leave the pty open so that we can write out the rlogin
948 * protocol for /bin/login, if the authentication works.
956 #endif /* AUTHENTICATION */
963 * Open the specified slave side of the pty,
964 * making sure that we have a clean tty.
972 * Make sure that other people can't open the
973 * slave side of the connection.
975 (void) chown(li, 0, 0);
976 (void) chmod(li, 0600);
980 t = open(line, O_RDWR|O_NOCTTY);
991 * Given a hostname, do whatever
992 * is necessary to startup the login process on the slave side of the pty.
997 startslave(char *host, int autologin, char *autoname)
1001 #ifdef AUTHENTICATION
1002 if (!autoname || !autoname[0])
1005 if (autologin < auth_level) {
1006 fatal(net, "Authorization failed");
1012 if ((i = fork()) < 0)
1013 fatalperror(net, "fork");
1017 start_login(host, autologin, autoname);
1028 if ((*envp = getenv("TZ")))
1038 * Assuming that we are now running as a child processes, this
1039 * function will turn us into the login process.
1042 #ifndef AUTHENTICATION
1043 #define undef1 __unused
1049 start_login(char *host undef1, int autologin undef1, char *name undef1)
1056 * -h : pass on name of host.
1057 * WARNING: -h is accepted by login if and only if
1059 * -p : don't clobber the environment (so terminal type stays set).
1061 * -f : force this login, he has already been authenticated
1063 argv = addarg(0, "login");
1065 #if !defined(NO_LOGIN_H)
1066 #ifdef AUTHENTICATION
1067 # if defined(NO_LOGIN_F) && defined(LOGIN_R)
1069 * Don't add the "-h host" option if we are going
1070 * to be adding the "-r host" option down below...
1072 if ((auth_level < 0) || (autologin != AUTH_VALID))
1075 argv = addarg(argv, "-h");
1076 argv = addarg(argv, host);
1078 #endif /* AUTHENTICATION */
1080 #if !defined(NO_LOGIN_P)
1081 argv = addarg(argv, "-p");
1085 * Set the environment variable "LINEMODE" to either
1086 * "real" or "kludge" if we are operating in either
1087 * real or kludge linemode.
1089 if (lmodetype == REAL_LINEMODE) {
1090 if (setenv("LINEMODE", "real", 1) == -1)
1091 syslog(LOG_ERR, "setenv: cannot set LINEMODE=real: %m");
1093 # ifdef KLUDGELINEMODE
1094 else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK) {
1095 if (setenv("LINEMODE", "kludge", 1) == -1)
1096 syslog(LOG_ERR, "setenv: cannot set LINEMODE=kludge: %m");
1102 * Are we working as the bftp daemon? If so, then ask login
1103 * to start bftp instead of shell.
1106 argv = addarg(argv, "-e");
1107 argv = addarg(argv, BFTPPATH);
1110 #ifdef AUTHENTICATION
1111 if (auth_level >= 0 && autologin == AUTH_VALID) {
1112 # if !defined(NO_LOGIN_F)
1113 argv = addarg(argv, "-f");
1114 argv = addarg(argv, "--");
1115 argv = addarg(argv, name);
1117 # if defined(LOGIN_R)
1119 * We don't have support for "login -f", but we
1120 * can fool /bin/login into thinking that we are
1121 * rlogind, and allow us to log in without a
1122 * password. The rlogin protocol expects
1123 * local-user\0remote-user\0term/speed\0
1129 int isecho, israw, xpty, len;
1130 extern int def_rspeed;
1133 * Tell login that we are coming from "localhost".
1134 * If we passed in the real host name, then the
1135 * user would have to allow .rhost access from
1136 * every machine that they want authenticated
1137 * access to work from, which sort of defeats
1138 * the purpose of an authenticated login...
1139 * So, we tell login that the session is coming
1140 * from "localhost", and the user will only have
1141 * to have "localhost" in their .rhost file.
1143 # define LOGIN_HOST "localhost"
1145 argv = addarg(argv, "-r");
1146 argv = addarg(argv, LOGIN_HOST);
1151 isecho = tty_isecho();
1152 israw = tty_israw();
1153 if (isecho || !israw) {
1154 tty_setecho(0); /* Turn off echo */
1155 tty_setraw(1); /* Turn on raw */
1158 len = strlen(name)+1;
1159 write(xpty, name, len);
1160 write(xpty, name, len);
1161 snprintf(speed, sizeof(speed),
1162 "%s/%d", (cp = getenv("TERM")) ? cp : "",
1163 (def_rspeed > 0) ? def_rspeed : 9600);
1164 len = strlen(speed)+1;
1165 write(xpty, speed, len);
1167 if (isecho || !israw) {
1169 tty_setecho(isecho);
1174 * Write a newline to ensure
1175 * that login will be able to
1178 write(xpty, "\n", 1);
1184 argv = addarg(argv, "--");
1185 argv = addarg(argv, name);
1190 if (getenv("USER")) {
1191 argv = addarg(argv, "--");
1192 argv = addarg(argv, getenv("USER"));
1193 #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1196 for (cpp = environ; *cpp; cpp++)
1197 argv = addarg(argv, *cpp);
1201 * Assume that login will set the USER variable
1202 * correctly. For SysV systems, this means that
1203 * USER will no longer be set, just LOGNAME by
1204 * login. (The problem is that if the auto-login
1205 * fails, and the user then specifies a different
1206 * account name, he can get logged in with both
1207 * LOGNAME and USER in his environment, but the
1208 * USER value will be wrong.
1212 #ifdef AUTHENTICATION
1213 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
1217 #endif /* AUTHENTICATION */
1220 if (altlogin == NULL) {
1221 altlogin = _PATH_LOGIN;
1223 execv(altlogin, argv);
1225 syslog(LOG_ERR, "%s: %m", altlogin);
1226 fatalperror(net, altlogin);
1231 addarg(char **argv, const char *val)
1237 * 10 entries, a leading length, and a null
1239 argv = (char **)malloc(sizeof(*argv) * 12);
1242 *argv++ = (char *)10;
1245 for (cpp = argv; *cpp; cpp++)
1247 if (cpp == &argv[(long)argv[-1]]) {
1249 *argv = (char *)((long)(*argv) + 10);
1250 argv = (char **)realloc(argv, sizeof(*argv)*((long)(*argv) + 2));
1254 cpp = &argv[(long)argv[-1] - 10];
1256 *cpp++ = strdup(val);
1264 * We only accept the environment variables listed below.
1269 static const char *rej[] = {
1274 static const char *acc[] = {
1275 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
1287 char ** new_environ;
1290 /* Allocate space for scrubbed environment. */
1291 for (count = 1, cpp = environ; *cpp; count++, cpp++)
1293 if ((new_environ = malloc(count * sizeof(char *))) == NULL) {
1298 for (cpp2 = new_environ, cpp = environ; *cpp; cpp++) {
1301 for(p = rej; *p; p++)
1302 if(strncmp(*cpp, *p, strlen(*p)) == 0) {
1309 for(p = acc; *p; p++)
1310 if(strncmp(*cpp, *p, strlen(*p)) == 0)
1313 if ((*cpp2++ = strdup(*cpp)) == NULL) {
1314 environ = new_environ;
1320 environ = new_environ;
1326 * This is the routine to call when we are all through, to
1327 * clean up anything that needs to be cleaned up.
1331 cleanup(int sig __unused)
1336 p = line + sizeof(_PATH_DEV) - 1;
1338 * Block all signals before clearing the utmp entry. We don't want to
1339 * be called again after calling logout() and then not add the wtmp
1340 * entry because of not finding the corresponding entry in utmp.
1343 sigprocmask(SIG_SETMASK, &mask, NULL);
1346 (void)chmod(line, 0666);
1347 (void)chown(line, 0, 0);
1349 (void)chmod(line, 0666);
1350 (void)chown(line, 0, 0);
1351 (void) shutdown(net, SHUT_RDWR);