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$");
42 #include <sys/types.h>
49 #include "pathnames.h"
52 #include <libtelnet/auth.h>
55 int cleanopen(char *);
61 char wtmpf[] = _PATH_WTMP;
63 char wtmpf[] = "/var/log/wtmp";
66 char utmpf[] = _PATH_UTMP;
68 char utmpf[] = "/var/run/utmp";
72 extern char **environ;
74 #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
75 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
102 # define cfsetospeed(tp, val) (tp)->sg.sg_ospeed = (val)
103 # define cfsetispeed(tp, val) (tp)->sg.sg_ispeed = (val)
104 # define cfgetospeed(tp) (tp)->sg.sg_ospeed
105 # define cfgetispeed(tp) (tp)->sg.sg_ispeed
106 #else /* USE_TERMIO */
109 # define TCSANOW TCSETS
110 # define TCSADRAIN TCSETSW
111 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
114 # define TCSANOW TCSETA
115 # define TCSADRAIN TCSETAW
116 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
118 # define TCSANOW TIOCSETA
119 # define TCSADRAIN TIOCSETAW
120 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
123 # define tcsetattr(f, a, t) ioctl(f, a, t)
124 # define cfsetospeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
125 (tp)->c_cflag |= (val)
126 # define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)
128 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CIBAUD; \
129 (tp)->c_cflag |= ((val)<<IBSHIFT)
130 # define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
132 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
133 (tp)->c_cflag |= (val)
134 # define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)
136 # endif /* TCSANOW */
137 struct termios termbuf, termbuf2; /* pty control structure */
138 #endif /* USE_TERMIO */
140 #include <sys/types.h>
143 int cleanopen(char *);
144 void scrub_env(void);
145 static char **addarg(char **, const char *);
152 * These three routines are used to get and set the "termbuf" structure
153 * to and from the kernel. init_termbuf() gets the current settings.
154 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
155 * set_termbuf() writes the structure into the kernel.
162 (void) ioctl(pty, TIOCGETP, (char *)&termbuf.sg);
163 (void) ioctl(pty, TIOCGETC, (char *)&termbuf.tc);
164 (void) ioctl(pty, TIOCGLTC, (char *)&termbuf.ltc);
166 (void) ioctl(pty, TIOCGSTATE, (char *)&termbuf.state);
169 (void) tcgetattr(pty, &termbuf);
174 #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
176 copy_termbuf(char *cp, size_t len)
178 if (len > sizeof(termbuf))
179 len = sizeof(termbuf);
180 memmove((char *)&termbuf, cp, len);
183 #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
189 * Only make the necessary changes.
192 if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg,
194 (void) ioctl(pty, TIOCSETN, (char *)&termbuf.sg);
195 if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc,
197 (void) ioctl(pty, TIOCSETC, (char *)&termbuf.tc);
198 if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc,
199 sizeof(termbuf.ltc)))
200 (void) ioctl(pty, TIOCSLTC, (char *)&termbuf.ltc);
201 if (termbuf.lflags != termbuf2.lflags)
202 (void) ioctl(pty, TIOCLSET, (char *)&termbuf.lflags);
203 #else /* USE_TERMIO */
204 if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
205 (void) tcsetattr(pty, TCSANOW, &termbuf);
206 #endif /* USE_TERMIO */
211 * spcset(func, valp, valpp)
213 * This function takes various special characters (func), and
214 * sets *valp to the current value of that character, and
215 * *valpp to point to where in the "termbuf" structure that
218 * It returns the SLC_ level of support for this function.
223 spcset(int func, cc_t *valp, cc_t **valpp)
227 *valp = termbuf.tc.t_eofc;
228 *valpp = (cc_t *)&termbuf.tc.t_eofc;
229 return(SLC_VARIABLE);
231 *valp = termbuf.sg.sg_erase;
232 *valpp = (cc_t *)&termbuf.sg.sg_erase;
233 return(SLC_VARIABLE);
235 *valp = termbuf.sg.sg_kill;
236 *valpp = (cc_t *)&termbuf.sg.sg_kill;
237 return(SLC_VARIABLE);
239 *valp = termbuf.tc.t_intrc;
240 *valpp = (cc_t *)&termbuf.tc.t_intrc;
241 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
243 *valp = termbuf.tc.t_quitc;
244 *valpp = (cc_t *)&termbuf.tc.t_quitc;
245 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
247 *valp = termbuf.tc.t_startc;
248 *valpp = (cc_t *)&termbuf.tc.t_startc;
249 return(SLC_VARIABLE);
251 *valp = termbuf.tc.t_stopc;
252 *valpp = (cc_t *)&termbuf.tc.t_stopc;
253 return(SLC_VARIABLE);
255 *valp = termbuf.ltc.t_flushc;
256 *valpp = (cc_t *)&termbuf.ltc.t_flushc;
257 return(SLC_VARIABLE);
259 *valp = termbuf.ltc.t_suspc;
260 *valpp = (cc_t *)&termbuf.ltc.t_suspc;
261 return(SLC_VARIABLE);
263 *valp = termbuf.ltc.t_werasc;
264 *valpp = (cc_t *)&termbuf.ltc.t_werasc;
265 return(SLC_VARIABLE);
267 *valp = termbuf.ltc.t_rprntc;
268 *valpp = (cc_t *)&termbuf.ltc.t_rprntc;
269 return(SLC_VARIABLE);
271 *valp = termbuf.ltc.t_lnextc;
272 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
273 return(SLC_VARIABLE);
275 *valp = termbuf.tc.t_brkc;
276 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
277 return(SLC_VARIABLE);
288 return(SLC_NOSUPPORT);
292 #else /* USE_TERMIO */
295 #define setval(a, b) *valp = termbuf.c_cc[a]; \
296 *valpp = &termbuf.c_cc[a]; \
298 #define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT);
301 spcset(int func, cc_t *valp, cc_t **valpp)
305 setval(VEOF, SLC_VARIABLE);
307 setval(VERASE, SLC_VARIABLE);
309 setval(VKILL, SLC_VARIABLE);
311 setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
313 setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
316 setval(VSTART, SLC_VARIABLE);
322 setval(VSTOP, SLC_VARIABLE);
328 setval(VWERASE, SLC_VARIABLE);
334 setval(VREPRINT, SLC_VARIABLE);
340 setval(VLNEXT, SLC_VARIABLE);
345 #if !defined(VDISCARD) && defined(VFLUSHO)
346 # define VDISCARD VFLUSHO
349 setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
355 setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
361 setval(VEOL, SLC_VARIABLE);
365 setval(VEOL2, SLC_VARIABLE);
369 setval(VSTATUS, SLC_VARIABLE);
382 return(SLC_NOSUPPORT);
385 #endif /* USE_TERMIO */
390 * Allocate a pty. As a side effect, the external character
391 * array "line" contains the name of the slave side.
393 * Returns the file descriptor of the opened pty.
395 char alpha[] = "0123456789abcdefghijklmnopqrstuv";
399 getpty(int *ptynum __unused)
406 (void) strcpy(line, _PATH_DEV);
407 (void) strcat(line, "ptyXX");
411 for (cp = "pqrsPQRS"; *cp; cp++) {
417 * This stat() check is just to keep us from
418 * looping through all 256 combinations if there
419 * aren't that many ptys available.
421 if (stat(line, &stb) < 0)
423 for (i = 0; i < 32; i++) {
439 * tty_flowmode() Find out if flow control is enabled or disabled.
440 * tty_linemode() Find out if linemode (external processing) is enabled.
441 * tty_setlinemod(on) Turn on/off linemode.
442 * tty_isecho() Find out if echoing is turned on.
443 * tty_setecho(on) Enable/disable character echoing.
444 * tty_israw() Find out if terminal is in RAW mode.
445 * tty_binaryin(on) Turn on/off BINARY on input.
446 * tty_binaryout(on) Turn on/off BINARY on output.
447 * tty_isediting() Find out if line editing is enabled.
448 * tty_istrapsig() Find out if signal trapping is enabled.
449 * tty_setedit(on) Turn on/off line editing.
450 * tty_setsig(on) Turn on/off signal trapping.
451 * tty_issofttab() Find out if tab expansion is enabled.
452 * tty_setsofttab(on) Turn on/off soft tab expansion.
453 * tty_islitecho() Find out if typed control chars are echoed literally
454 * tty_setlitecho() Turn on/off literal echo of control chars
455 * tty_tspeed(val) Set transmit speed to val.
456 * tty_rspeed(val) Set receive speed to val.
464 return(termbuf.state & TS_EXTPROC);
466 return(termbuf.c_lflag & EXTPROC);
471 tty_setlinemode(int on)
475 (void) ioctl(pty, TIOCEXT, (char *)&on);
480 termbuf.c_lflag |= EXTPROC;
482 termbuf.c_lflag &= ~EXTPROC;
486 #endif /* LINEMODE */
492 return (termbuf.sg.sg_flags & ECHO);
494 return (termbuf.c_lflag & ECHO);
502 return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0);
504 return((termbuf.c_iflag & IXON) ? 1 : 0);
513 return((termbuf.lflags & DECCTQ) ? 0 : 1);
518 return((termbuf.c_iflag & IXANY) ? 1 : 0);
527 termbuf.sg.sg_flags |= ECHO|CRMOD;
529 termbuf.sg.sg_flags &= ~(ECHO|CRMOD);
532 termbuf.c_lflag |= ECHO;
534 termbuf.c_lflag &= ~ECHO;
542 return(termbuf.sg.sg_flags & RAW);
544 return(!(termbuf.c_lflag & ICANON));
548 #ifdef AUTHENTICATION
549 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
555 termbuf.sg.sg_flags |= RAW;
557 termbuf.sg.sg_flags &= ~RAW;
560 termbuf.c_lflag &= ~ICANON;
562 termbuf.c_lflag |= ICANON;
566 #endif /* AUTHENTICATION */
573 termbuf.lflags |= LPASS8;
575 termbuf.lflags &= ~LPASS8;
578 termbuf.c_iflag &= ~ISTRIP;
580 termbuf.c_iflag |= ISTRIP;
586 tty_binaryout(int on)
590 termbuf.lflags |= LLITOUT;
592 termbuf.lflags &= ~LLITOUT;
595 termbuf.c_cflag &= ~(CSIZE|PARENB);
596 termbuf.c_cflag |= CS8;
597 termbuf.c_oflag &= ~OPOST;
599 termbuf.c_cflag &= ~CSIZE;
600 termbuf.c_cflag |= CS7|PARENB;
601 termbuf.c_oflag |= OPOST;
610 return(termbuf.lflags & LPASS8);
612 return(!(termbuf.c_iflag & ISTRIP));
617 tty_isbinaryout(void)
620 return(termbuf.lflags & LLITOUT);
622 return(!(termbuf.c_oflag&OPOST));
631 return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));
633 return(termbuf.c_lflag & ICANON);
641 return(!(termbuf.sg.sg_flags&RAW));
643 return(termbuf.c_lflag & ISIG);
652 termbuf.sg.sg_flags &= ~CBREAK;
654 termbuf.sg.sg_flags |= CBREAK;
657 termbuf.c_lflag |= ICANON;
659 termbuf.c_lflag &= ~ICANON;
671 termbuf.c_lflag |= ISIG;
673 termbuf.c_lflag &= ~ISIG;
676 #endif /* LINEMODE */
682 return (termbuf.sg.sg_flags & XTABS);
685 return (termbuf.c_oflag & OXTABS);
688 return ((termbuf.c_oflag & TABDLY) == TAB3);
694 tty_setsofttab(int on)
698 termbuf.sg.sg_flags |= XTABS;
700 termbuf.sg.sg_flags &= ~XTABS;
704 termbuf.c_oflag |= OXTABS;
707 termbuf.c_oflag &= ~TABDLY;
708 termbuf.c_oflag |= TAB3;
712 termbuf.c_oflag &= ~OXTABS;
715 termbuf.c_oflag &= ~TABDLY;
716 termbuf.c_oflag |= TAB0;
726 return (!(termbuf.lflags & LCTLECH));
729 return (!(termbuf.c_lflag & ECHOCTL));
732 return (!(termbuf.c_lflag & TCTLECH));
734 # if !defined(ECHOCTL) && !defined(TCTLECH)
735 return (0); /* assumes ctl chars are echoed '^x' */
741 tty_setlitecho(int on)
745 termbuf.lflags &= ~LCTLECH;
747 termbuf.lflags |= LCTLECH;
751 termbuf.c_lflag &= ~ECHOCTL;
753 termbuf.c_lflag |= ECHOCTL;
757 termbuf.c_lflag &= ~TCTLECH;
759 termbuf.c_lflag |= TCTLECH;
768 return (termbuf.sg.sg_flags & CRMOD);
770 return (termbuf.c_iflag & ICRNL);
775 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
784 * A table of available terminal speeds
790 { 0, B0 }, { 50, B50 }, { 75, B75 },
791 { 110, B110 }, { 134, B134 }, { 150, B150 },
792 { 200, B200 }, { 300, B300 }, { 600, B600 },
793 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
822 #endif /* DECODE_BAUD */
828 struct termspeeds *tp;
830 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
832 if (tp->speed == -1) /* back up to last valid value */
834 cfsetospeed(&termbuf, tp->value);
835 #else /* DECODE_BAUD */
836 cfsetospeed(&termbuf, val);
837 #endif /* DECODE_BAUD */
844 struct termspeeds *tp;
846 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
848 if (tp->speed == -1) /* back up to last valid value */
850 cfsetispeed(&termbuf, tp->value);
851 #else /* DECODE_BAUD */
852 cfsetispeed(&termbuf, val);
853 #endif /* DECODE_BAUD */
859 * Open the slave side of the pty, and do any initialization
873 extern int def_row, def_col;
875 extern int def_tspeed, def_rspeed;
877 * Opening the slave side may cause initilization of the
878 * kernel tty structure. We need remember the state of
879 * if linemode was turned on
880 * terminal window size
883 * so that we can re-set them if we need to.
886 waslm = tty_linemode();
888 erase = termbuf.c_cc[VERASE];
891 * Make sure that we don't have a controlling tty, and
892 * that we are the session (process group) leader.
895 t = open(_PATH_TTY, O_RDWR);
897 (void) ioctl(t, TIOCNOTTY, (char *)0);
904 fatalperror(net, line);
908 * set up the tty modes as we like them to be.
912 if (def_row || def_col) {
913 memset((char *)&ws, 0, sizeof(ws));
916 (void)ioctl(t, TIOCSWINSZ, (char *)&ws);
921 * Settings for sgtty based systems
924 termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
925 # endif /* USE_TERMIO */
928 * Settings for all other termios/termio based
929 * systems, other than 4.4BSD. In 4.4BSD the
930 * kernel does the initial terminal setup.
932 tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
933 tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
935 termbuf.c_cc[VERASE] = erase;
939 # endif /* LINEMODE */
942 * Set the tty modes, and make this our controlling tty.
945 if (login_tty(t) == -1)
946 fatalperror(net, "login_tty");
949 #ifdef AUTHENTICATION
950 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
952 * Leave the pty open so that we can write out the rlogin
953 * protocol for /bin/login, if the authentication works.
961 #endif /* AUTHENTICATION */
968 * Open the specified slave side of the pty,
969 * making sure that we have a clean tty.
977 * Make sure that other people can't open the
978 * slave side of the connection.
980 (void) chown(li, 0, 0);
981 (void) chmod(li, 0600);
985 t = open(line, O_RDWR|O_NOCTTY);
996 * Given a hostname, do whatever
997 * is necessary to startup the login process on the slave side of the pty.
1002 startslave(char *host, int autologin, char *autoname)
1006 #ifdef AUTHENTICATION
1007 if (!autoname || !autoname[0])
1010 if (autologin < auth_level) {
1011 fatal(net, "Authorization failed");
1017 if ((i = fork()) < 0)
1018 fatalperror(net, "fork");
1022 start_login(host, autologin, autoname);
1033 if ((*envp = getenv("TZ")))
1043 * Assuming that we are now running as a child processes, this
1044 * function will turn us into the login process.
1047 #ifndef AUTHENTICATION
1048 #define undef1 __unused
1054 start_login(char *host undef1, int autologin undef1, char *name undef1)
1061 * -h : pass on name of host.
1062 * WARNING: -h is accepted by login if and only if
1064 * -p : don't clobber the environment (so terminal type stays set).
1066 * -f : force this login, he has already been authenticated
1068 argv = addarg(0, "login");
1070 #if !defined(NO_LOGIN_H)
1071 #ifdef AUTHENTICATION
1072 # if defined(NO_LOGIN_F) && defined(LOGIN_R)
1074 * Don't add the "-h host" option if we are going
1075 * to be adding the "-r host" option down below...
1077 if ((auth_level < 0) || (autologin != AUTH_VALID))
1080 argv = addarg(argv, "-h");
1081 argv = addarg(argv, host);
1083 #endif /* AUTHENTICATION */
1085 #if !defined(NO_LOGIN_P)
1086 argv = addarg(argv, "-p");
1090 * Set the environment variable "LINEMODE" to either
1091 * "real" or "kludge" if we are operating in either
1092 * real or kludge linemode.
1094 if (lmodetype == REAL_LINEMODE)
1095 setenv("LINEMODE", "real", 1);
1096 # ifdef KLUDGELINEMODE
1097 else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK)
1098 setenv("LINEMODE", "kludge", 1);
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=",
1289 for (cpp2 = cpp = environ; *cpp; cpp++) {
1292 for(p = rej; *p; p++)
1293 if(strncmp(*cpp, *p, strlen(*p)) == 0) {
1300 for(p = acc; *p; p++)
1301 if(strncmp(*cpp, *p, strlen(*p)) == 0)
1312 * This is the routine to call when we are all through, to
1313 * clean up anything that needs to be cleaned up.
1317 cleanup(int sig __unused)
1322 p = line + sizeof(_PATH_DEV) - 1;
1324 * Block all signals before clearing the utmp entry. We don't want to
1325 * be called again after calling logout() and then not add the wtmp
1326 * entry because of not finding the corresponding entry in utmp.
1329 sigprocmask(SIG_SETMASK, &mask, NULL);
1332 (void)chmod(line, 0666);
1333 (void)chown(line, 0, 0);
1335 (void)chmod(line, 0666);
1336 (void)chown(line, 0, 0);
1337 (void) shutdown(net, 2);