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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)sys_term.c 8.4+1 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/telnetd/sys_term.c,v 1.7.2.5 2002/06/17 02:48:02 jmallett Exp $
33 #include <sys/types.h>
40 #include "pathnames.h"
43 #include <libtelnet/auth.h>
46 int cleanopen(char *);
50 extern char **environ;
52 #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
53 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
80 # define cfsetospeed(tp, val) (tp)->sg.sg_ospeed = (val)
81 # define cfsetispeed(tp, val) (tp)->sg.sg_ispeed = (val)
82 # define cfgetospeed(tp) (tp)->sg.sg_ospeed
83 # define cfgetispeed(tp) (tp)->sg.sg_ispeed
84 #else /* USE_TERMIO */
87 # define TCSANOW TCSETS
88 # define TCSADRAIN TCSETSW
89 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
92 # define TCSANOW TCSETA
93 # define TCSADRAIN TCSETAW
94 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
96 # define TCSANOW TIOCSETA
97 # define TCSADRAIN TIOCSETAW
98 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
101 # define tcsetattr(f, a, t) ioctl(f, a, t)
102 # define cfsetospeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
103 (tp)->c_cflag |= (val)
104 # define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)
106 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CIBAUD; \
107 (tp)->c_cflag |= ((val)<<IBSHIFT)
108 # define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
110 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
111 (tp)->c_cflag |= (val)
112 # define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)
114 # endif /* TCSANOW */
115 struct termios termbuf, termbuf2; /* pty control structure */
116 #endif /* USE_TERMIO */
118 int cleanopen(char *);
119 void scrub_env(void);
120 static char **addarg(char **, const char *);
127 * These three routines are used to get and set the "termbuf" structure
128 * to and from the kernel. init_termbuf() gets the current settings.
129 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
130 * set_termbuf() writes the structure into the kernel.
137 (void) ioctl(pty, TIOCGETP, (char *)&termbuf.sg);
138 (void) ioctl(pty, TIOCGETC, (char *)&termbuf.tc);
139 (void) ioctl(pty, TIOCGLTC, (char *)&termbuf.ltc);
141 (void) ioctl(pty, TIOCGSTATE, (char *)&termbuf.state);
144 (void) tcgetattr(pty, &termbuf);
149 #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
151 copy_termbuf(char *cp, size_t len)
153 if (len > sizeof(termbuf))
154 len = sizeof(termbuf);
155 memmove((char *)&termbuf, cp, len);
158 #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
164 * Only make the necessary changes.
167 if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg,
169 (void) ioctl(pty, TIOCSETN, (char *)&termbuf.sg);
170 if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc,
172 (void) ioctl(pty, TIOCSETC, (char *)&termbuf.tc);
173 if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc,
174 sizeof(termbuf.ltc)))
175 (void) ioctl(pty, TIOCSLTC, (char *)&termbuf.ltc);
176 if (termbuf.lflags != termbuf2.lflags)
177 (void) ioctl(pty, TIOCLSET, (char *)&termbuf.lflags);
178 #else /* USE_TERMIO */
179 if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
180 (void) tcsetattr(pty, TCSANOW, &termbuf);
181 #endif /* USE_TERMIO */
186 * spcset(func, valp, valpp)
188 * This function takes various special characters (func), and
189 * sets *valp to the current value of that character, and
190 * *valpp to point to where in the "termbuf" structure that
193 * It returns the SLC_ level of support for this function.
198 spcset(int func, cc_t *valp, cc_t **valpp)
202 *valp = termbuf.tc.t_eofc;
203 *valpp = (cc_t *)&termbuf.tc.t_eofc;
204 return(SLC_VARIABLE);
206 *valp = termbuf.sg.sg_erase;
207 *valpp = (cc_t *)&termbuf.sg.sg_erase;
208 return(SLC_VARIABLE);
210 *valp = termbuf.sg.sg_kill;
211 *valpp = (cc_t *)&termbuf.sg.sg_kill;
212 return(SLC_VARIABLE);
214 *valp = termbuf.tc.t_intrc;
215 *valpp = (cc_t *)&termbuf.tc.t_intrc;
216 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
218 *valp = termbuf.tc.t_quitc;
219 *valpp = (cc_t *)&termbuf.tc.t_quitc;
220 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
222 *valp = termbuf.tc.t_startc;
223 *valpp = (cc_t *)&termbuf.tc.t_startc;
224 return(SLC_VARIABLE);
226 *valp = termbuf.tc.t_stopc;
227 *valpp = (cc_t *)&termbuf.tc.t_stopc;
228 return(SLC_VARIABLE);
230 *valp = termbuf.ltc.t_flushc;
231 *valpp = (cc_t *)&termbuf.ltc.t_flushc;
232 return(SLC_VARIABLE);
234 *valp = termbuf.ltc.t_suspc;
235 *valpp = (cc_t *)&termbuf.ltc.t_suspc;
236 return(SLC_VARIABLE);
238 *valp = termbuf.ltc.t_werasc;
239 *valpp = (cc_t *)&termbuf.ltc.t_werasc;
240 return(SLC_VARIABLE);
242 *valp = termbuf.ltc.t_rprntc;
243 *valpp = (cc_t *)&termbuf.ltc.t_rprntc;
244 return(SLC_VARIABLE);
246 *valp = termbuf.ltc.t_lnextc;
247 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
248 return(SLC_VARIABLE);
250 *valp = termbuf.tc.t_brkc;
251 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
252 return(SLC_VARIABLE);
263 return(SLC_NOSUPPORT);
267 #else /* USE_TERMIO */
270 #define setval(a, b) *valp = termbuf.c_cc[a]; \
271 *valpp = &termbuf.c_cc[a]; \
273 #define defval(a) *valp = ((cc_t)a); *valpp = NULL; return(SLC_DEFAULT);
276 spcset(int func, cc_t *valp, cc_t **valpp)
280 setval(VEOF, SLC_VARIABLE);
282 setval(VERASE, SLC_VARIABLE);
284 setval(VKILL, SLC_VARIABLE);
286 setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
288 setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
291 setval(VSTART, SLC_VARIABLE);
297 setval(VSTOP, SLC_VARIABLE);
303 setval(VWERASE, SLC_VARIABLE);
309 setval(VREPRINT, SLC_VARIABLE);
315 setval(VLNEXT, SLC_VARIABLE);
320 #if !defined(VDISCARD) && defined(VFLUSHO)
321 # define VDISCARD VFLUSHO
324 setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
330 setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
336 setval(VEOL, SLC_VARIABLE);
340 setval(VEOL2, SLC_VARIABLE);
344 setval(VSTATUS, SLC_VARIABLE);
357 return(SLC_NOSUPPORT);
360 #endif /* USE_TERMIO */
365 * Allocate a pty. As a side effect, the external character
366 * array "line" contains the name of the slave side.
368 * Returns the file descriptor of the opened pty.
370 char alpha[] = "0123456789abcdefghijklmnopqrstuv";
374 getpty(int *ptynum __unused)
381 (void) strcpy(line, _PATH_DEV);
382 (void) strcat(line, "ptyXX");
386 for (cp = "pqrsPQRS"; *cp; cp++) {
392 * This stat() check is just to keep us from
393 * looping through all 256 combinations if there
394 * aren't that many ptys available.
396 if (stat(line, &stb) < 0)
398 for (i = 0; i < 32; i++) {
414 * tty_flowmode() Find out if flow control is enabled or disabled.
415 * tty_linemode() Find out if linemode (external processing) is enabled.
416 * tty_setlinemod(on) Turn on/off linemode.
417 * tty_isecho() Find out if echoing is turned on.
418 * tty_setecho(on) Enable/disable character echoing.
419 * tty_israw() Find out if terminal is in RAW mode.
420 * tty_binaryin(on) Turn on/off BINARY on input.
421 * tty_binaryout(on) Turn on/off BINARY on output.
422 * tty_isediting() Find out if line editing is enabled.
423 * tty_istrapsig() Find out if signal trapping is enabled.
424 * tty_setedit(on) Turn on/off line editing.
425 * tty_setsig(on) Turn on/off signal trapping.
426 * tty_issofttab() Find out if tab expansion is enabled.
427 * tty_setsofttab(on) Turn on/off soft tab expansion.
428 * tty_islitecho() Find out if typed control chars are echoed literally
429 * tty_setlitecho() Turn on/off literal echo of control chars
430 * tty_tspeed(val) Set transmit speed to val.
431 * tty_rspeed(val) Set receive speed to val.
439 return(termbuf.state & TS_EXTPROC);
441 return(termbuf.c_lflag & EXTPROC);
446 tty_setlinemode(int on)
450 (void) ioctl(pty, TIOCEXT, (char *)&on);
455 termbuf.c_lflag |= EXTPROC;
457 termbuf.c_lflag &= ~EXTPROC;
461 #endif /* LINEMODE */
467 return (termbuf.sg.sg_flags & ECHO);
469 return (termbuf.c_lflag & ECHO);
477 return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0);
479 return((termbuf.c_iflag & IXON) ? 1 : 0);
488 return((termbuf.lflags & DECCTQ) ? 0 : 1);
493 return((termbuf.c_iflag & IXANY) ? 1 : 0);
502 termbuf.sg.sg_flags |= ECHO|CRMOD;
504 termbuf.sg.sg_flags &= ~(ECHO|CRMOD);
507 termbuf.c_lflag |= ECHO;
509 termbuf.c_lflag &= ~ECHO;
517 return(termbuf.sg.sg_flags & RAW);
519 return(!(termbuf.c_lflag & ICANON));
523 #ifdef AUTHENTICATION
524 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
530 termbuf.sg.sg_flags |= RAW;
532 termbuf.sg.sg_flags &= ~RAW;
535 termbuf.c_lflag &= ~ICANON;
537 termbuf.c_lflag |= ICANON;
541 #endif /* AUTHENTICATION */
548 termbuf.lflags |= LPASS8;
550 termbuf.lflags &= ~LPASS8;
553 termbuf.c_iflag &= ~ISTRIP;
555 termbuf.c_iflag |= ISTRIP;
561 tty_binaryout(int on)
565 termbuf.lflags |= LLITOUT;
567 termbuf.lflags &= ~LLITOUT;
570 termbuf.c_cflag &= ~(CSIZE|PARENB);
571 termbuf.c_cflag |= CS8;
572 termbuf.c_oflag &= ~OPOST;
574 termbuf.c_cflag &= ~CSIZE;
575 termbuf.c_cflag |= CS7|PARENB;
576 termbuf.c_oflag |= OPOST;
585 return(termbuf.lflags & LPASS8);
587 return(!(termbuf.c_iflag & ISTRIP));
592 tty_isbinaryout(void)
595 return(termbuf.lflags & LLITOUT);
597 return(!(termbuf.c_oflag&OPOST));
606 return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));
608 return(termbuf.c_lflag & ICANON);
616 return(!(termbuf.sg.sg_flags&RAW));
618 return(termbuf.c_lflag & ISIG);
627 termbuf.sg.sg_flags &= ~CBREAK;
629 termbuf.sg.sg_flags |= CBREAK;
632 termbuf.c_lflag |= ICANON;
634 termbuf.c_lflag &= ~ICANON;
646 termbuf.c_lflag |= ISIG;
648 termbuf.c_lflag &= ~ISIG;
651 #endif /* LINEMODE */
657 return (termbuf.sg.sg_flags & XTABS);
660 return (termbuf.c_oflag & OXTABS);
663 return ((termbuf.c_oflag & TABDLY) == TAB3);
669 tty_setsofttab(int on)
673 termbuf.sg.sg_flags |= XTABS;
675 termbuf.sg.sg_flags &= ~XTABS;
679 termbuf.c_oflag |= OXTABS;
682 termbuf.c_oflag &= ~TABDLY;
683 termbuf.c_oflag |= TAB3;
687 termbuf.c_oflag &= ~OXTABS;
690 termbuf.c_oflag &= ~TABDLY;
691 termbuf.c_oflag |= TAB0;
701 return (!(termbuf.lflags & LCTLECH));
704 return (!(termbuf.c_lflag & ECHOCTL));
707 return (!(termbuf.c_lflag & TCTLECH));
709 # if !defined(ECHOCTL) && !defined(TCTLECH)
710 return (0); /* assumes ctl chars are echoed '^x' */
716 tty_setlitecho(int on)
720 termbuf.lflags &= ~LCTLECH;
722 termbuf.lflags |= LCTLECH;
726 termbuf.c_lflag &= ~ECHOCTL;
728 termbuf.c_lflag |= ECHOCTL;
732 termbuf.c_lflag &= ~TCTLECH;
734 termbuf.c_lflag |= TCTLECH;
743 return (termbuf.sg.sg_flags & CRMOD);
745 return (termbuf.c_iflag & ICRNL);
750 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
759 * A table of available terminal speeds
765 { 0, B0 }, { 50, B50 }, { 75, B75 },
766 { 110, B110 }, { 134, B134 }, { 150, B150 },
767 { 200, B200 }, { 300, B300 }, { 600, B600 },
768 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
797 #endif /* DECODE_BAUD */
803 struct termspeeds *tp;
805 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
807 if (tp->speed == -1) /* back up to last valid value */
809 cfsetospeed(&termbuf, tp->value);
810 #else /* DECODE_BAUD */
811 cfsetospeed(&termbuf, val);
812 #endif /* DECODE_BAUD */
819 struct termspeeds *tp;
821 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
823 if (tp->speed == -1) /* back up to last valid value */
825 cfsetispeed(&termbuf, tp->value);
826 #else /* DECODE_BAUD */
827 cfsetispeed(&termbuf, val);
828 #endif /* DECODE_BAUD */
834 * Open the slave side of the pty, and do any initialization
848 extern int def_row, def_col;
850 extern int def_tspeed, def_rspeed;
852 * Opening the slave side may cause initilization of the
853 * kernel tty structure. We need remember the state of
854 * if linemode was turned on
855 * terminal window size
858 * so that we can re-set them if we need to.
861 waslm = tty_linemode();
863 erase = termbuf.c_cc[VERASE];
866 * Make sure that we don't have a controlling tty, and
867 * that we are the session (process group) leader.
870 t = open(_PATH_TTY, O_RDWR);
872 (void) ioctl(t, TIOCNOTTY, NULL);
879 fatalperror(net, line);
883 * set up the tty modes as we like them to be.
887 if (def_row || def_col) {
888 memset((char *)&ws, 0, sizeof(ws));
891 (void)ioctl(t, TIOCSWINSZ, (char *)&ws);
896 * Settings for sgtty based systems
899 termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
900 # endif /* USE_TERMIO */
903 * Settings for all other termios/termio based
904 * systems, other than 4.4BSD. In 4.4BSD the
905 * kernel does the initial terminal setup.
907 tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
908 tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
910 termbuf.c_cc[VERASE] = erase;
914 # endif /* LINEMODE */
917 * Set the tty modes, and make this our controlling tty.
920 if (login_tty(t) == -1)
921 fatalperror(net, "login_tty");
924 #ifdef AUTHENTICATION
925 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
927 * Leave the pty open so that we can write out the rlogin
928 * protocol for /bin/login, if the authentication works.
936 #endif /* AUTHENTICATION */
943 * Open the specified slave side of the pty,
944 * making sure that we have a clean tty.
952 * Make sure that other people can't open the
953 * slave side of the connection.
955 (void) chown(li, 0, 0);
956 (void) chmod(li, 0600);
960 t = open(line, O_RDWR|O_NOCTTY);
971 * Given a hostname, do whatever
972 * is necessary to startup the login process on the slave side of the pty.
977 startslave(char *host, int autologin, char *autoname)
981 #ifdef AUTHENTICATION
982 if (!autoname || !autoname[0])
985 if (autologin < auth_level) {
986 fatalmsg(net, "Authorization failed");
992 if ((i = fork()) < 0)
993 fatalperror(net, "fork");
997 start_login(host, autologin, autoname);
1008 if ((*envp = getenv("TZ")))
1018 * Assuming that we are now running as a child processes, this
1019 * function will turn us into the login process.
1022 #ifndef AUTHENTICATION
1023 #define undef1 __unused
1029 start_login(char *host undef1, int autologin undef1, char *name undef1)
1036 * -h : pass on name of host.
1037 * WARNING: -h is accepted by login if and only if
1039 * -p : don't clobber the environment (so terminal type stays set).
1041 * -f : force this login, he has already been authenticated
1043 argv = addarg(0, "login");
1045 #if !defined(NO_LOGIN_H)
1046 #ifdef AUTHENTICATION
1047 # if defined(NO_LOGIN_F) && defined(LOGIN_R)
1049 * Don't add the "-h host" option if we are going
1050 * to be adding the "-r host" option down below...
1052 if ((auth_level < 0) || (autologin != AUTH_VALID))
1055 argv = addarg(argv, "-h");
1056 argv = addarg(argv, host);
1058 #endif /* AUTHENTICATION */
1060 #if !defined(NO_LOGIN_P)
1061 argv = addarg(argv, "-p");
1065 * Set the environment variable "LINEMODE" to either
1066 * "real" or "kludge" if we are operating in either
1067 * real or kludge linemode.
1069 if (lmodetype == REAL_LINEMODE) {
1070 if (setenv("LINEMODE", "real", 1) == -1)
1071 syslog(LOG_ERR, "setenv: cannot set LINEMODE=real: %m");
1073 # ifdef KLUDGELINEMODE
1074 else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK) {
1075 if (setenv("LINEMODE", "kludge", 1) == -1)
1076 syslog(LOG_ERR, "setenv: cannot set LINEMODE=kludge: %m");
1082 * Are we working as the bftp daemon? If so, then ask login
1083 * to start bftp instead of shell.
1086 argv = addarg(argv, "-e");
1087 argv = addarg(argv, BFTPPATH);
1090 #ifdef AUTHENTICATION
1091 if (auth_level >= 0 && autologin == AUTH_VALID) {
1092 # if !defined(NO_LOGIN_F)
1093 argv = addarg(argv, "-f");
1094 argv = addarg(argv, "--");
1095 argv = addarg(argv, name);
1097 # if defined(LOGIN_R)
1099 * We don't have support for "login -f", but we
1100 * can fool /bin/login into thinking that we are
1101 * rlogind, and allow us to log in without a
1102 * password. The rlogin protocol expects
1103 * local-user\0remote-user\0term/speed\0
1109 int isecho, israw, xpty, len;
1110 extern int def_rspeed;
1113 * Tell login that we are coming from "localhost".
1114 * If we passed in the real host name, then the
1115 * user would have to allow .rhost access from
1116 * every machine that they want authenticated
1117 * access to work from, which sort of defeats
1118 * the purpose of an authenticated login...
1119 * So, we tell login that the session is coming
1120 * from "localhost", and the user will only have
1121 * to have "localhost" in their .rhost file.
1123 # define LOGIN_HOST "localhost"
1125 argv = addarg(argv, "-r");
1126 argv = addarg(argv, LOGIN_HOST);
1131 isecho = tty_isecho();
1132 israw = tty_israw();
1133 if (isecho || !israw) {
1134 tty_setecho(0); /* Turn off echo */
1135 tty_setraw(1); /* Turn on raw */
1138 len = strlen(name)+1;
1139 write(xpty, name, len);
1140 write(xpty, name, len);
1141 snprintf(speed, sizeof(speed),
1142 "%s/%d", (cp = getenv("TERM")) ? cp : "",
1143 (def_rspeed > 0) ? def_rspeed : 9600);
1144 len = strlen(speed)+1;
1145 write(xpty, speed, len);
1147 if (isecho || !israw) {
1149 tty_setecho(isecho);
1154 * Write a newline to ensure
1155 * that login will be able to
1158 write(xpty, "\n", 1);
1164 argv = addarg(argv, "--");
1165 argv = addarg(argv, name);
1170 if (getenv("USER")) {
1171 argv = addarg(argv, "--");
1172 argv = addarg(argv, getenv("USER"));
1173 #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1176 for (cpp = environ; *cpp; cpp++)
1177 argv = addarg(argv, *cpp);
1181 * Assume that login will set the USER variable
1182 * correctly. For SysV systems, this means that
1183 * USER will no longer be set, just LOGNAME by
1184 * login. (The problem is that if the auto-login
1185 * fails, and the user then specifies a different
1186 * account name, he can get logged in with both
1187 * LOGNAME and USER in his environment, but the
1188 * USER value will be wrong.
1192 #ifdef AUTHENTICATION
1193 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
1197 #endif /* AUTHENTICATION */
1200 if (altlogin == NULL) {
1201 altlogin = _PATH_LOGIN;
1203 execv(altlogin, argv);
1205 syslog(LOG_ERR, "%s: %m", altlogin);
1206 fatalperror(net, altlogin);
1211 addarg(char **argv, const char *val)
1217 * 10 entries, a leading length, and a null
1219 argv = (char **)malloc(sizeof(*argv) * 12);
1222 *argv++ = (char *)10;
1225 for (cpp = argv; *cpp; cpp++)
1227 if (cpp == &argv[(long)argv[-1]]) {
1229 *argv = (char *)((long)(*argv) + 10);
1230 argv = (char **)realloc(argv, sizeof(*argv)*((long)(*argv) + 2));
1234 cpp = &argv[(long)argv[-1] - 10];
1236 *cpp++ = strdup(val);
1244 * We only accept the environment variables listed below.
1249 static const char *rej[] = {
1254 static const char *acc[] = {
1255 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
1267 char ** new_environ;
1270 /* Allocate space for scrubbed environment. */
1271 for (count = 1, cpp = environ; *cpp; count++, cpp++)
1273 if ((new_environ = malloc(count * sizeof(char *))) == NULL) {
1278 for (cpp2 = new_environ, cpp = environ; *cpp; cpp++) {
1281 for(p = rej; *p; p++)
1282 if(strncmp(*cpp, *p, strlen(*p)) == 0) {
1289 for(p = acc; *p; p++)
1290 if(strncmp(*cpp, *p, strlen(*p)) == 0)
1293 if ((*cpp2++ = strdup(*cpp)) == NULL) {
1294 environ = new_environ;
1300 environ = new_environ;
1306 * This is the routine to call when we are all through, to
1307 * clean up anything that needs to be cleaned up.
1311 cleanup(int sig __unused)
1316 p = line + sizeof(_PATH_DEV) - 1;
1318 * Block all signals before clearing the utmpx entry. We don't want to
1319 * be called again after calling logout() and then not add the wtmpx
1320 * entry because of not finding the corresponding entry in utmpx.
1323 sigprocmask(SIG_SETMASK, &mask, NULL);
1324 if (logoutx(p, 0, DEAD_PROCESS))
1325 logwtmpx(p, "", "", 0, DEAD_PROCESS);
1326 (void)chmod(line, 0666);
1327 (void)chown(line, 0, 0);
1329 (void)chmod(line, 0666);
1330 (void)chown(line, 0, 0);
1331 (void) shutdown(net, SHUT_RDWR);