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 *);
52 char wtmpf[] = _PATH_WTMP;
54 char wtmpf[] = "/var/log/wtmp";
57 char utmpf[] = _PATH_UTMP;
59 char utmpf[] = "/var/run/utmp";
63 extern char **environ;
65 #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
66 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
93 # define cfsetospeed(tp, val) (tp)->sg.sg_ospeed = (val)
94 # define cfsetispeed(tp, val) (tp)->sg.sg_ispeed = (val)
95 # define cfgetospeed(tp) (tp)->sg.sg_ospeed
96 # define cfgetispeed(tp) (tp)->sg.sg_ispeed
97 #else /* USE_TERMIO */
100 # define TCSANOW TCSETS
101 # define TCSADRAIN TCSETSW
102 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
105 # define TCSANOW TCSETA
106 # define TCSADRAIN TCSETAW
107 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
109 # define TCSANOW TIOCSETA
110 # define TCSADRAIN TIOCSETAW
111 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
114 # define tcsetattr(f, a, t) ioctl(f, a, t)
115 # define cfsetospeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
116 (tp)->c_cflag |= (val)
117 # define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)
119 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CIBAUD; \
120 (tp)->c_cflag |= ((val)<<IBSHIFT)
121 # define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
123 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
124 (tp)->c_cflag |= (val)
125 # define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)
127 # endif /* TCSANOW */
128 struct termios termbuf, termbuf2; /* pty control structure */
129 #endif /* USE_TERMIO */
131 int cleanopen(char *);
132 void scrub_env(void);
133 static char **addarg(char **, const char *);
140 * These three routines are used to get and set the "termbuf" structure
141 * to and from the kernel. init_termbuf() gets the current settings.
142 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
143 * set_termbuf() writes the structure into the kernel.
150 (void) ioctl(pty, TIOCGETP, (char *)&termbuf.sg);
151 (void) ioctl(pty, TIOCGETC, (char *)&termbuf.tc);
152 (void) ioctl(pty, TIOCGLTC, (char *)&termbuf.ltc);
154 (void) ioctl(pty, TIOCGSTATE, (char *)&termbuf.state);
157 (void) tcgetattr(pty, &termbuf);
162 #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
164 copy_termbuf(char *cp, size_t len)
166 if (len > sizeof(termbuf))
167 len = sizeof(termbuf);
168 memmove((char *)&termbuf, cp, len);
171 #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
177 * Only make the necessary changes.
180 if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg,
182 (void) ioctl(pty, TIOCSETN, (char *)&termbuf.sg);
183 if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc,
185 (void) ioctl(pty, TIOCSETC, (char *)&termbuf.tc);
186 if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc,
187 sizeof(termbuf.ltc)))
188 (void) ioctl(pty, TIOCSLTC, (char *)&termbuf.ltc);
189 if (termbuf.lflags != termbuf2.lflags)
190 (void) ioctl(pty, TIOCLSET, (char *)&termbuf.lflags);
191 #else /* USE_TERMIO */
192 if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
193 (void) tcsetattr(pty, TCSANOW, &termbuf);
194 #endif /* USE_TERMIO */
199 * spcset(func, valp, valpp)
201 * This function takes various special characters (func), and
202 * sets *valp to the current value of that character, and
203 * *valpp to point to where in the "termbuf" structure that
206 * It returns the SLC_ level of support for this function.
211 spcset(int func, cc_t *valp, cc_t **valpp)
215 *valp = termbuf.tc.t_eofc;
216 *valpp = (cc_t *)&termbuf.tc.t_eofc;
217 return(SLC_VARIABLE);
219 *valp = termbuf.sg.sg_erase;
220 *valpp = (cc_t *)&termbuf.sg.sg_erase;
221 return(SLC_VARIABLE);
223 *valp = termbuf.sg.sg_kill;
224 *valpp = (cc_t *)&termbuf.sg.sg_kill;
225 return(SLC_VARIABLE);
227 *valp = termbuf.tc.t_intrc;
228 *valpp = (cc_t *)&termbuf.tc.t_intrc;
229 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
231 *valp = termbuf.tc.t_quitc;
232 *valpp = (cc_t *)&termbuf.tc.t_quitc;
233 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
235 *valp = termbuf.tc.t_startc;
236 *valpp = (cc_t *)&termbuf.tc.t_startc;
237 return(SLC_VARIABLE);
239 *valp = termbuf.tc.t_stopc;
240 *valpp = (cc_t *)&termbuf.tc.t_stopc;
241 return(SLC_VARIABLE);
243 *valp = termbuf.ltc.t_flushc;
244 *valpp = (cc_t *)&termbuf.ltc.t_flushc;
245 return(SLC_VARIABLE);
247 *valp = termbuf.ltc.t_suspc;
248 *valpp = (cc_t *)&termbuf.ltc.t_suspc;
249 return(SLC_VARIABLE);
251 *valp = termbuf.ltc.t_werasc;
252 *valpp = (cc_t *)&termbuf.ltc.t_werasc;
253 return(SLC_VARIABLE);
255 *valp = termbuf.ltc.t_rprntc;
256 *valpp = (cc_t *)&termbuf.ltc.t_rprntc;
257 return(SLC_VARIABLE);
259 *valp = termbuf.ltc.t_lnextc;
260 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
261 return(SLC_VARIABLE);
263 *valp = termbuf.tc.t_brkc;
264 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
265 return(SLC_VARIABLE);
276 return(SLC_NOSUPPORT);
280 #else /* USE_TERMIO */
283 #define setval(a, b) *valp = termbuf.c_cc[a]; \
284 *valpp = &termbuf.c_cc[a]; \
286 #define defval(a) *valp = ((cc_t)a); *valpp = NULL; return(SLC_DEFAULT);
289 spcset(int func, cc_t *valp, cc_t **valpp)
293 setval(VEOF, SLC_VARIABLE);
295 setval(VERASE, SLC_VARIABLE);
297 setval(VKILL, SLC_VARIABLE);
299 setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
301 setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
304 setval(VSTART, SLC_VARIABLE);
310 setval(VSTOP, SLC_VARIABLE);
316 setval(VWERASE, SLC_VARIABLE);
322 setval(VREPRINT, SLC_VARIABLE);
328 setval(VLNEXT, SLC_VARIABLE);
333 #if !defined(VDISCARD) && defined(VFLUSHO)
334 # define VDISCARD VFLUSHO
337 setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
343 setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
349 setval(VEOL, SLC_VARIABLE);
353 setval(VEOL2, SLC_VARIABLE);
357 setval(VSTATUS, SLC_VARIABLE);
370 return(SLC_NOSUPPORT);
373 #endif /* USE_TERMIO */
378 * Allocate a pty. As a side effect, the external character
379 * array "line" contains the name of the slave side.
381 * Returns the file descriptor of the opened pty.
383 char alpha[] = "0123456789abcdefghijklmnopqrstuv";
387 getpty(int *ptynum __unused)
394 (void) strcpy(line, _PATH_DEV);
395 (void) strcat(line, "ptyXX");
399 for (cp = "pqrsPQRS"; *cp; cp++) {
405 * This stat() check is just to keep us from
406 * looping through all 256 combinations if there
407 * aren't that many ptys available.
409 if (stat(line, &stb) < 0)
411 for (i = 0; i < 32; i++) {
427 * tty_flowmode() Find out if flow control is enabled or disabled.
428 * tty_linemode() Find out if linemode (external processing) is enabled.
429 * tty_setlinemod(on) Turn on/off linemode.
430 * tty_isecho() Find out if echoing is turned on.
431 * tty_setecho(on) Enable/disable character echoing.
432 * tty_israw() Find out if terminal is in RAW mode.
433 * tty_binaryin(on) Turn on/off BINARY on input.
434 * tty_binaryout(on) Turn on/off BINARY on output.
435 * tty_isediting() Find out if line editing is enabled.
436 * tty_istrapsig() Find out if signal trapping is enabled.
437 * tty_setedit(on) Turn on/off line editing.
438 * tty_setsig(on) Turn on/off signal trapping.
439 * tty_issofttab() Find out if tab expansion is enabled.
440 * tty_setsofttab(on) Turn on/off soft tab expansion.
441 * tty_islitecho() Find out if typed control chars are echoed literally
442 * tty_setlitecho() Turn on/off literal echo of control chars
443 * tty_tspeed(val) Set transmit speed to val.
444 * tty_rspeed(val) Set receive speed to val.
452 return(termbuf.state & TS_EXTPROC);
454 return(termbuf.c_lflag & EXTPROC);
459 tty_setlinemode(int on)
463 (void) ioctl(pty, TIOCEXT, (char *)&on);
468 termbuf.c_lflag |= EXTPROC;
470 termbuf.c_lflag &= ~EXTPROC;
474 #endif /* LINEMODE */
480 return (termbuf.sg.sg_flags & ECHO);
482 return (termbuf.c_lflag & ECHO);
490 return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0);
492 return((termbuf.c_iflag & IXON) ? 1 : 0);
501 return((termbuf.lflags & DECCTQ) ? 0 : 1);
506 return((termbuf.c_iflag & IXANY) ? 1 : 0);
515 termbuf.sg.sg_flags |= ECHO|CRMOD;
517 termbuf.sg.sg_flags &= ~(ECHO|CRMOD);
520 termbuf.c_lflag |= ECHO;
522 termbuf.c_lflag &= ~ECHO;
530 return(termbuf.sg.sg_flags & RAW);
532 return(!(termbuf.c_lflag & ICANON));
536 #ifdef AUTHENTICATION
537 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
543 termbuf.sg.sg_flags |= RAW;
545 termbuf.sg.sg_flags &= ~RAW;
548 termbuf.c_lflag &= ~ICANON;
550 termbuf.c_lflag |= ICANON;
554 #endif /* AUTHENTICATION */
561 termbuf.lflags |= LPASS8;
563 termbuf.lflags &= ~LPASS8;
566 termbuf.c_iflag &= ~ISTRIP;
568 termbuf.c_iflag |= ISTRIP;
574 tty_binaryout(int on)
578 termbuf.lflags |= LLITOUT;
580 termbuf.lflags &= ~LLITOUT;
583 termbuf.c_cflag &= ~(CSIZE|PARENB);
584 termbuf.c_cflag |= CS8;
585 termbuf.c_oflag &= ~OPOST;
587 termbuf.c_cflag &= ~CSIZE;
588 termbuf.c_cflag |= CS7|PARENB;
589 termbuf.c_oflag |= OPOST;
598 return(termbuf.lflags & LPASS8);
600 return(!(termbuf.c_iflag & ISTRIP));
605 tty_isbinaryout(void)
608 return(termbuf.lflags & LLITOUT);
610 return(!(termbuf.c_oflag&OPOST));
619 return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));
621 return(termbuf.c_lflag & ICANON);
629 return(!(termbuf.sg.sg_flags&RAW));
631 return(termbuf.c_lflag & ISIG);
640 termbuf.sg.sg_flags &= ~CBREAK;
642 termbuf.sg.sg_flags |= CBREAK;
645 termbuf.c_lflag |= ICANON;
647 termbuf.c_lflag &= ~ICANON;
659 termbuf.c_lflag |= ISIG;
661 termbuf.c_lflag &= ~ISIG;
664 #endif /* LINEMODE */
670 return (termbuf.sg.sg_flags & XTABS);
673 return (termbuf.c_oflag & OXTABS);
676 return ((termbuf.c_oflag & TABDLY) == TAB3);
682 tty_setsofttab(int on)
686 termbuf.sg.sg_flags |= XTABS;
688 termbuf.sg.sg_flags &= ~XTABS;
692 termbuf.c_oflag |= OXTABS;
695 termbuf.c_oflag &= ~TABDLY;
696 termbuf.c_oflag |= TAB3;
700 termbuf.c_oflag &= ~OXTABS;
703 termbuf.c_oflag &= ~TABDLY;
704 termbuf.c_oflag |= TAB0;
714 return (!(termbuf.lflags & LCTLECH));
717 return (!(termbuf.c_lflag & ECHOCTL));
720 return (!(termbuf.c_lflag & TCTLECH));
722 # if !defined(ECHOCTL) && !defined(TCTLECH)
723 return (0); /* assumes ctl chars are echoed '^x' */
729 tty_setlitecho(int on)
733 termbuf.lflags &= ~LCTLECH;
735 termbuf.lflags |= LCTLECH;
739 termbuf.c_lflag &= ~ECHOCTL;
741 termbuf.c_lflag |= ECHOCTL;
745 termbuf.c_lflag &= ~TCTLECH;
747 termbuf.c_lflag |= TCTLECH;
756 return (termbuf.sg.sg_flags & CRMOD);
758 return (termbuf.c_iflag & ICRNL);
763 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
772 * A table of available terminal speeds
778 { 0, B0 }, { 50, B50 }, { 75, B75 },
779 { 110, B110 }, { 134, B134 }, { 150, B150 },
780 { 200, B200 }, { 300, B300 }, { 600, B600 },
781 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
810 #endif /* DECODE_BAUD */
816 struct termspeeds *tp;
818 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
820 if (tp->speed == -1) /* back up to last valid value */
822 cfsetospeed(&termbuf, tp->value);
823 #else /* DECODE_BAUD */
824 cfsetospeed(&termbuf, val);
825 #endif /* DECODE_BAUD */
832 struct termspeeds *tp;
834 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
836 if (tp->speed == -1) /* back up to last valid value */
838 cfsetispeed(&termbuf, tp->value);
839 #else /* DECODE_BAUD */
840 cfsetispeed(&termbuf, val);
841 #endif /* DECODE_BAUD */
847 * Open the slave side of the pty, and do any initialization
861 extern int def_row, def_col;
863 extern int def_tspeed, def_rspeed;
865 * Opening the slave side may cause initilization of the
866 * kernel tty structure. We need remember the state of
867 * if linemode was turned on
868 * terminal window size
871 * so that we can re-set them if we need to.
874 waslm = tty_linemode();
876 erase = termbuf.c_cc[VERASE];
879 * Make sure that we don't have a controlling tty, and
880 * that we are the session (process group) leader.
883 t = open(_PATH_TTY, O_RDWR);
885 (void) ioctl(t, TIOCNOTTY, NULL);
892 fatalperror(net, line);
896 * set up the tty modes as we like them to be.
900 if (def_row || def_col) {
901 memset((char *)&ws, 0, sizeof(ws));
904 (void)ioctl(t, TIOCSWINSZ, (char *)&ws);
909 * Settings for sgtty based systems
912 termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
913 # endif /* USE_TERMIO */
916 * Settings for all other termios/termio based
917 * systems, other than 4.4BSD. In 4.4BSD the
918 * kernel does the initial terminal setup.
920 tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
921 tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
923 termbuf.c_cc[VERASE] = erase;
927 # endif /* LINEMODE */
930 * Set the tty modes, and make this our controlling tty.
933 if (login_tty(t) == -1)
934 fatalperror(net, "login_tty");
937 #ifdef AUTHENTICATION
938 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
940 * Leave the pty open so that we can write out the rlogin
941 * protocol for /bin/login, if the authentication works.
949 #endif /* AUTHENTICATION */
956 * Open the specified slave side of the pty,
957 * making sure that we have a clean tty.
965 * Make sure that other people can't open the
966 * slave side of the connection.
968 (void) chown(li, 0, 0);
969 (void) chmod(li, 0600);
973 t = open(line, O_RDWR|O_NOCTTY);
984 * Given a hostname, do whatever
985 * is necessary to startup the login process on the slave side of the pty.
990 startslave(char *host, int autologin, char *autoname)
994 #ifdef AUTHENTICATION
995 if (!autoname || !autoname[0])
998 if (autologin < auth_level) {
999 fatal(net, "Authorization failed");
1005 if ((i = fork()) < 0)
1006 fatalperror(net, "fork");
1010 start_login(host, autologin, autoname);
1021 if ((*envp = getenv("TZ")))
1031 * Assuming that we are now running as a child processes, this
1032 * function will turn us into the login process.
1035 #ifndef AUTHENTICATION
1036 #define undef1 __unused
1042 start_login(char *host undef1, int autologin undef1, char *name undef1)
1049 * -h : pass on name of host.
1050 * WARNING: -h is accepted by login if and only if
1052 * -p : don't clobber the environment (so terminal type stays set).
1054 * -f : force this login, he has already been authenticated
1056 argv = addarg(0, "login");
1058 #if !defined(NO_LOGIN_H)
1059 #ifdef AUTHENTICATION
1060 # if defined(NO_LOGIN_F) && defined(LOGIN_R)
1062 * Don't add the "-h host" option if we are going
1063 * to be adding the "-r host" option down below...
1065 if ((auth_level < 0) || (autologin != AUTH_VALID))
1068 argv = addarg(argv, "-h");
1069 argv = addarg(argv, host);
1071 #endif /* AUTHENTICATION */
1073 #if !defined(NO_LOGIN_P)
1074 argv = addarg(argv, "-p");
1078 * Set the environment variable "LINEMODE" to either
1079 * "real" or "kludge" if we are operating in either
1080 * real or kludge linemode.
1082 if (lmodetype == REAL_LINEMODE) {
1083 if (setenv("LINEMODE", "real", 1) == -1)
1084 syslog(LOG_ERR, "setenv: cannot set LINEMODE=real: %m");
1086 # ifdef KLUDGELINEMODE
1087 else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK) {
1088 if (setenv("LINEMODE", "kludge", 1) == -1)
1089 syslog(LOG_ERR, "setenv: cannot set LINEMODE=kludge: %m");
1095 * Are we working as the bftp daemon? If so, then ask login
1096 * to start bftp instead of shell.
1099 argv = addarg(argv, "-e");
1100 argv = addarg(argv, BFTPPATH);
1103 #ifdef AUTHENTICATION
1104 if (auth_level >= 0 && autologin == AUTH_VALID) {
1105 # if !defined(NO_LOGIN_F)
1106 argv = addarg(argv, "-f");
1107 argv = addarg(argv, "--");
1108 argv = addarg(argv, name);
1110 # if defined(LOGIN_R)
1112 * We don't have support for "login -f", but we
1113 * can fool /bin/login into thinking that we are
1114 * rlogind, and allow us to log in without a
1115 * password. The rlogin protocol expects
1116 * local-user\0remote-user\0term/speed\0
1122 int isecho, israw, xpty, len;
1123 extern int def_rspeed;
1126 * Tell login that we are coming from "localhost".
1127 * If we passed in the real host name, then the
1128 * user would have to allow .rhost access from
1129 * every machine that they want authenticated
1130 * access to work from, which sort of defeats
1131 * the purpose of an authenticated login...
1132 * So, we tell login that the session is coming
1133 * from "localhost", and the user will only have
1134 * to have "localhost" in their .rhost file.
1136 # define LOGIN_HOST "localhost"
1138 argv = addarg(argv, "-r");
1139 argv = addarg(argv, LOGIN_HOST);
1144 isecho = tty_isecho();
1145 israw = tty_israw();
1146 if (isecho || !israw) {
1147 tty_setecho(0); /* Turn off echo */
1148 tty_setraw(1); /* Turn on raw */
1151 len = strlen(name)+1;
1152 write(xpty, name, len);
1153 write(xpty, name, len);
1154 snprintf(speed, sizeof(speed),
1155 "%s/%d", (cp = getenv("TERM")) ? cp : "",
1156 (def_rspeed > 0) ? def_rspeed : 9600);
1157 len = strlen(speed)+1;
1158 write(xpty, speed, len);
1160 if (isecho || !israw) {
1162 tty_setecho(isecho);
1167 * Write a newline to ensure
1168 * that login will be able to
1171 write(xpty, "\n", 1);
1177 argv = addarg(argv, "--");
1178 argv = addarg(argv, name);
1183 if (getenv("USER")) {
1184 argv = addarg(argv, "--");
1185 argv = addarg(argv, getenv("USER"));
1186 #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1189 for (cpp = environ; *cpp; cpp++)
1190 argv = addarg(argv, *cpp);
1194 * Assume that login will set the USER variable
1195 * correctly. For SysV systems, this means that
1196 * USER will no longer be set, just LOGNAME by
1197 * login. (The problem is that if the auto-login
1198 * fails, and the user then specifies a different
1199 * account name, he can get logged in with both
1200 * LOGNAME and USER in his environment, but the
1201 * USER value will be wrong.
1205 #ifdef AUTHENTICATION
1206 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
1210 #endif /* AUTHENTICATION */
1213 if (altlogin == NULL) {
1214 altlogin = _PATH_LOGIN;
1216 execv(altlogin, argv);
1218 syslog(LOG_ERR, "%s: %m", altlogin);
1219 fatalperror(net, altlogin);
1224 addarg(char **argv, const char *val)
1230 * 10 entries, a leading length, and a null
1232 argv = (char **)malloc(sizeof(*argv) * 12);
1235 *argv++ = (char *)10;
1238 for (cpp = argv; *cpp; cpp++)
1240 if (cpp == &argv[(long)argv[-1]]) {
1242 *argv = (char *)((long)(*argv) + 10);
1243 argv = (char **)realloc(argv, sizeof(*argv)*((long)(*argv) + 2));
1247 cpp = &argv[(long)argv[-1] - 10];
1249 *cpp++ = strdup(val);
1257 * We only accept the environment variables listed below.
1262 static const char *rej[] = {
1267 static const char *acc[] = {
1268 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
1280 char ** new_environ;
1283 /* Allocate space for scrubbed environment. */
1284 for (count = 1, cpp = environ; *cpp; count++, cpp++)
1286 if ((new_environ = malloc(count * sizeof(char *))) == NULL) {
1291 for (cpp2 = new_environ, cpp = environ; *cpp; cpp++) {
1294 for(p = rej; *p; p++)
1295 if(strncmp(*cpp, *p, strlen(*p)) == 0) {
1302 for(p = acc; *p; p++)
1303 if(strncmp(*cpp, *p, strlen(*p)) == 0)
1306 if ((*cpp2++ = strdup(*cpp)) == NULL) {
1307 environ = new_environ;
1313 environ = new_environ;
1319 * This is the routine to call when we are all through, to
1320 * clean up anything that needs to be cleaned up.
1324 cleanup(int sig __unused)
1329 p = line + sizeof(_PATH_DEV) - 1;
1331 * Block all signals before clearing the utmp entry. We don't want to
1332 * be called again after calling logout() and then not add the wtmp
1333 * entry because of not finding the corresponding entry in utmp.
1336 sigprocmask(SIG_SETMASK, &mask, NULL);
1339 (void)chmod(line, 0666);
1340 (void)chown(line, 0, 0);
1342 (void)chmod(line, 0666);
1343 (void)chown(line, 0, 0);
1344 (void) shutdown(net, SHUT_RDWR);