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
32 static const char sccsid[] = "@(#)sys_term.c 8.4+1 (Berkeley) 5/30/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
44 #include "pathnames.h"
49 #include <libtelnet/auth.h>
52 int cleanopen(char *);
56 extern char **environ;
58 #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
59 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
86 # define cfsetospeed(tp, val) (tp)->sg.sg_ospeed = (val)
87 # define cfsetispeed(tp, val) (tp)->sg.sg_ispeed = (val)
88 # define cfgetospeed(tp) (tp)->sg.sg_ospeed
89 # define cfgetispeed(tp) (tp)->sg.sg_ispeed
90 #else /* USE_TERMIO */
93 # define TCSANOW TCSETS
94 # define TCSADRAIN TCSETSW
95 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
98 # define TCSANOW TCSETA
99 # define TCSADRAIN TCSETAW
100 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
102 # define TCSANOW TIOCSETA
103 # define TCSADRAIN TIOCSETAW
104 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
107 # define tcsetattr(f, a, t) ioctl(f, a, t)
108 # define cfsetospeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
109 (tp)->c_cflag |= (val)
110 # define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)
112 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CIBAUD; \
113 (tp)->c_cflag |= ((val)<<IBSHIFT)
114 # define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
116 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
117 (tp)->c_cflag |= (val)
118 # define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)
120 # endif /* TCSANOW */
121 struct termios termbuf, termbuf2; /* pty control structure */
122 #endif /* USE_TERMIO */
124 #include <sys/types.h>
127 int cleanopen(char *);
128 void scrub_env(void);
129 static char **addarg(char **, const char *);
136 * These three routines are used to get and set the "termbuf" structure
137 * to and from the kernel. init_termbuf() gets the current settings.
138 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
139 * set_termbuf() writes the structure into the kernel.
146 (void) ioctl(pty, TIOCGETP, (char *)&termbuf.sg);
147 (void) ioctl(pty, TIOCGETC, (char *)&termbuf.tc);
148 (void) ioctl(pty, TIOCGLTC, (char *)&termbuf.ltc);
150 (void) ioctl(pty, TIOCGSTATE, (char *)&termbuf.state);
153 (void) tcgetattr(pty, &termbuf);
158 #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
160 copy_termbuf(char *cp, size_t len)
162 if (len > sizeof(termbuf))
163 len = sizeof(termbuf);
164 memmove((char *)&termbuf, cp, len);
167 #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
173 * Only make the necessary changes.
176 if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg,
178 (void) ioctl(pty, TIOCSETN, (char *)&termbuf.sg);
179 if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc,
181 (void) ioctl(pty, TIOCSETC, (char *)&termbuf.tc);
182 if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc,
183 sizeof(termbuf.ltc)))
184 (void) ioctl(pty, TIOCSLTC, (char *)&termbuf.ltc);
185 if (termbuf.lflags != termbuf2.lflags)
186 (void) ioctl(pty, TIOCLSET, (char *)&termbuf.lflags);
187 #else /* USE_TERMIO */
188 if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
189 (void) tcsetattr(pty, TCSANOW, &termbuf);
190 #endif /* USE_TERMIO */
195 * spcset(func, valp, valpp)
197 * This function takes various special characters (func), and
198 * sets *valp to the current value of that character, and
199 * *valpp to point to where in the "termbuf" structure that
202 * It returns the SLC_ level of support for this function.
207 spcset(int func, cc_t *valp, cc_t **valpp)
211 *valp = termbuf.tc.t_eofc;
212 *valpp = (cc_t *)&termbuf.tc.t_eofc;
213 return(SLC_VARIABLE);
215 *valp = termbuf.sg.sg_erase;
216 *valpp = (cc_t *)&termbuf.sg.sg_erase;
217 return(SLC_VARIABLE);
219 *valp = termbuf.sg.sg_kill;
220 *valpp = (cc_t *)&termbuf.sg.sg_kill;
221 return(SLC_VARIABLE);
223 *valp = termbuf.tc.t_intrc;
224 *valpp = (cc_t *)&termbuf.tc.t_intrc;
225 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
227 *valp = termbuf.tc.t_quitc;
228 *valpp = (cc_t *)&termbuf.tc.t_quitc;
229 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
231 *valp = termbuf.tc.t_startc;
232 *valpp = (cc_t *)&termbuf.tc.t_startc;
233 return(SLC_VARIABLE);
235 *valp = termbuf.tc.t_stopc;
236 *valpp = (cc_t *)&termbuf.tc.t_stopc;
237 return(SLC_VARIABLE);
239 *valp = termbuf.ltc.t_flushc;
240 *valpp = (cc_t *)&termbuf.ltc.t_flushc;
241 return(SLC_VARIABLE);
243 *valp = termbuf.ltc.t_suspc;
244 *valpp = (cc_t *)&termbuf.ltc.t_suspc;
245 return(SLC_VARIABLE);
247 *valp = termbuf.ltc.t_werasc;
248 *valpp = (cc_t *)&termbuf.ltc.t_werasc;
249 return(SLC_VARIABLE);
251 *valp = termbuf.ltc.t_rprntc;
252 *valpp = (cc_t *)&termbuf.ltc.t_rprntc;
253 return(SLC_VARIABLE);
255 *valp = termbuf.ltc.t_lnextc;
256 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
257 return(SLC_VARIABLE);
259 *valp = termbuf.tc.t_brkc;
260 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
261 return(SLC_VARIABLE);
272 return(SLC_NOSUPPORT);
276 #else /* USE_TERMIO */
279 #define setval(a, b) *valp = termbuf.c_cc[a]; \
280 *valpp = &termbuf.c_cc[a]; \
282 #define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT);
285 spcset(int func, cc_t *valp, cc_t **valpp)
289 setval(VEOF, SLC_VARIABLE);
291 setval(VERASE, SLC_VARIABLE);
293 setval(VKILL, SLC_VARIABLE);
295 setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
297 setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
300 setval(VSTART, SLC_VARIABLE);
306 setval(VSTOP, SLC_VARIABLE);
312 setval(VWERASE, SLC_VARIABLE);
318 setval(VREPRINT, SLC_VARIABLE);
324 setval(VLNEXT, SLC_VARIABLE);
329 #if !defined(VDISCARD) && defined(VFLUSHO)
330 # define VDISCARD VFLUSHO
333 setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
339 setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
345 setval(VEOL, SLC_VARIABLE);
349 setval(VEOL2, SLC_VARIABLE);
353 setval(VSTATUS, SLC_VARIABLE);
366 return(SLC_NOSUPPORT);
369 #endif /* USE_TERMIO */
374 * Allocate a pty. As a side effect, the external character
375 * array "line" contains the name of the slave side.
377 * Returns the file descriptor of the opened pty.
382 getpty(int *ptynum __unused)
387 p = posix_openpt(O_RDWR|O_NOCTTY);
391 if (grantpt(p) == -1)
394 if (unlockpt(p) == -1)
401 if (strlcpy(line, pn, sizeof line) >= sizeof line)
409 * tty_flowmode() Find out if flow control is enabled or disabled.
410 * tty_linemode() Find out if linemode (external processing) is enabled.
411 * tty_setlinemod(on) Turn on/off linemode.
412 * tty_isecho() Find out if echoing is turned on.
413 * tty_setecho(on) Enable/disable character echoing.
414 * tty_israw() Find out if terminal is in RAW mode.
415 * tty_binaryin(on) Turn on/off BINARY on input.
416 * tty_binaryout(on) Turn on/off BINARY on output.
417 * tty_isediting() Find out if line editing is enabled.
418 * tty_istrapsig() Find out if signal trapping is enabled.
419 * tty_setedit(on) Turn on/off line editing.
420 * tty_setsig(on) Turn on/off signal trapping.
421 * tty_issofttab() Find out if tab expansion is enabled.
422 * tty_setsofttab(on) Turn on/off soft tab expansion.
423 * tty_islitecho() Find out if typed control chars are echoed literally
424 * tty_setlitecho() Turn on/off literal echo of control chars
425 * tty_tspeed(val) Set transmit speed to val.
426 * tty_rspeed(val) Set receive speed to val.
434 return(termbuf.state & TS_EXTPROC);
436 return(termbuf.c_lflag & EXTPROC);
441 tty_setlinemode(int on)
445 (void) ioctl(pty, TIOCEXT, (char *)&on);
450 termbuf.c_lflag |= EXTPROC;
452 termbuf.c_lflag &= ~EXTPROC;
456 #endif /* LINEMODE */
462 return (termbuf.sg.sg_flags & ECHO);
464 return (termbuf.c_lflag & ECHO);
472 return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0);
474 return((termbuf.c_iflag & IXON) ? 1 : 0);
483 return((termbuf.lflags & DECCTQ) ? 0 : 1);
488 return((termbuf.c_iflag & IXANY) ? 1 : 0);
497 termbuf.sg.sg_flags |= ECHO|CRMOD;
499 termbuf.sg.sg_flags &= ~(ECHO|CRMOD);
502 termbuf.c_lflag |= ECHO;
504 termbuf.c_lflag &= ~ECHO;
512 return(termbuf.sg.sg_flags & RAW);
514 return(!(termbuf.c_lflag & ICANON));
518 #ifdef AUTHENTICATION
519 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
525 termbuf.sg.sg_flags |= RAW;
527 termbuf.sg.sg_flags &= ~RAW;
530 termbuf.c_lflag &= ~ICANON;
532 termbuf.c_lflag |= ICANON;
536 #endif /* AUTHENTICATION */
543 termbuf.lflags |= LPASS8;
545 termbuf.lflags &= ~LPASS8;
548 termbuf.c_iflag &= ~ISTRIP;
550 termbuf.c_iflag |= ISTRIP;
556 tty_binaryout(int on)
560 termbuf.lflags |= LLITOUT;
562 termbuf.lflags &= ~LLITOUT;
565 termbuf.c_cflag &= ~(CSIZE|PARENB);
566 termbuf.c_cflag |= CS8;
567 termbuf.c_oflag &= ~OPOST;
569 termbuf.c_cflag &= ~CSIZE;
570 termbuf.c_cflag |= CS7|PARENB;
571 termbuf.c_oflag |= OPOST;
580 return(termbuf.lflags & LPASS8);
582 return(!(termbuf.c_iflag & ISTRIP));
587 tty_isbinaryout(void)
590 return(termbuf.lflags & LLITOUT);
592 return(!(termbuf.c_oflag&OPOST));
601 return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));
603 return(termbuf.c_lflag & ICANON);
611 return(!(termbuf.sg.sg_flags&RAW));
613 return(termbuf.c_lflag & ISIG);
622 termbuf.sg.sg_flags &= ~CBREAK;
624 termbuf.sg.sg_flags |= CBREAK;
627 termbuf.c_lflag |= ICANON;
629 termbuf.c_lflag &= ~ICANON;
641 termbuf.c_lflag |= ISIG;
643 termbuf.c_lflag &= ~ISIG;
646 #endif /* LINEMODE */
652 return (termbuf.sg.sg_flags & XTABS);
655 return (termbuf.c_oflag & OXTABS);
658 return ((termbuf.c_oflag & TABDLY) == TAB3);
664 tty_setsofttab(int on)
668 termbuf.sg.sg_flags |= XTABS;
670 termbuf.sg.sg_flags &= ~XTABS;
674 termbuf.c_oflag |= OXTABS;
677 termbuf.c_oflag &= ~TABDLY;
678 termbuf.c_oflag |= TAB3;
682 termbuf.c_oflag &= ~OXTABS;
685 termbuf.c_oflag &= ~TABDLY;
686 termbuf.c_oflag |= TAB0;
696 return (!(termbuf.lflags & LCTLECH));
699 return (!(termbuf.c_lflag & ECHOCTL));
702 return (!(termbuf.c_lflag & TCTLECH));
704 # if !defined(ECHOCTL) && !defined(TCTLECH)
705 return (0); /* assumes ctl chars are echoed '^x' */
711 tty_setlitecho(int on)
715 termbuf.lflags &= ~LCTLECH;
717 termbuf.lflags |= LCTLECH;
721 termbuf.c_lflag &= ~ECHOCTL;
723 termbuf.c_lflag |= ECHOCTL;
727 termbuf.c_lflag &= ~TCTLECH;
729 termbuf.c_lflag |= TCTLECH;
738 return (termbuf.sg.sg_flags & CRMOD);
740 return (termbuf.c_iflag & ICRNL);
748 struct termspeeds *tp;
750 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
752 if (tp->speed == -1) /* back up to last valid value */
754 cfsetospeed(&termbuf, tp->value);
755 #else /* DECODE_BAUD */
756 cfsetospeed(&termbuf, val);
757 #endif /* DECODE_BAUD */
764 struct termspeeds *tp;
766 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
768 if (tp->speed == -1) /* back up to last valid value */
770 cfsetispeed(&termbuf, tp->value);
771 #else /* DECODE_BAUD */
772 cfsetispeed(&termbuf, val);
773 #endif /* DECODE_BAUD */
779 * Open the slave side of the pty, and do any initialization
793 extern int def_row, def_col;
795 extern int def_tspeed, def_rspeed;
797 * Opening the slave side may cause initilization of the
798 * kernel tty structure. We need remember the state of
799 * if linemode was turned on
800 * terminal window size
803 * so that we can re-set them if we need to.
806 waslm = tty_linemode();
808 erase = termbuf.c_cc[VERASE];
811 * Make sure that we don't have a controlling tty, and
812 * that we are the session (process group) leader.
815 t = open(_PATH_TTY, O_RDWR);
817 (void) ioctl(t, TIOCNOTTY, (char *)0);
824 fatalperror(net, line);
828 * set up the tty modes as we like them to be.
832 if (def_row || def_col) {
833 memset((char *)&ws, 0, sizeof(ws));
836 (void)ioctl(t, TIOCSWINSZ, (char *)&ws);
841 * Settings for sgtty based systems
844 termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
845 # endif /* USE_TERMIO */
848 * Settings for all other termios/termio based
849 * systems, other than 4.4BSD. In 4.4BSD the
850 * kernel does the initial terminal setup.
852 tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
853 tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
855 termbuf.c_cc[VERASE] = erase;
859 # endif /* LINEMODE */
862 * Set the tty modes, and make this our controlling tty.
865 if (login_tty(t) == -1)
866 fatalperror(net, "login_tty");
869 #ifdef AUTHENTICATION
870 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
872 * Leave the pty open so that we can write out the rlogin
873 * protocol for /bin/login, if the authentication works.
881 #endif /* AUTHENTICATION */
888 * Open the specified slave side of the pty,
889 * making sure that we have a clean tty.
897 * Make sure that other people can't open the
898 * slave side of the connection.
900 (void) chown(li, 0, 0);
901 (void) chmod(li, 0600);
905 t = open(line, O_RDWR|O_NOCTTY);
916 * Given a hostname, do whatever
917 * is necessary to startup the login process on the slave side of the pty.
922 startslave(char *host, int autologin, char *autoname)
926 #ifdef AUTHENTICATION
927 if (!autoname || !autoname[0])
930 if (autologin < auth_level) {
931 fatal(net, "Authorization failed");
937 if ((i = fork()) < 0)
938 fatalperror(net, "fork");
942 start_login(host, autologin, autoname);
953 if ((*envp = getenv("TZ")))
963 * Assuming that we are now running as a child processes, this
964 * function will turn us into the login process.
967 #ifndef AUTHENTICATION
968 #define undef1 __unused
974 start_login(char *host undef1, int autologin undef1, char *name undef1)
979 user = getenv("USER");
980 user = (user != NULL) ? strdup(user) : NULL;
985 * -h : pass on name of host.
986 * WARNING: -h is accepted by login if and only if
988 * -p : don't clobber the environment (so terminal type stays set).
990 * -f : force this login, he has already been authenticated
992 argv = addarg(0, "login");
994 #if !defined(NO_LOGIN_H)
995 #ifdef AUTHENTICATION
996 # if defined(NO_LOGIN_F) && defined(LOGIN_R)
998 * Don't add the "-h host" option if we are going
999 * to be adding the "-r host" option down below...
1001 if ((auth_level < 0) || (autologin != AUTH_VALID))
1003 #endif /* AUTHENTICATION */
1005 argv = addarg(argv, "-h");
1006 argv = addarg(argv, host);
1009 #if !defined(NO_LOGIN_P)
1010 argv = addarg(argv, "-p");
1014 * Set the environment variable "LINEMODE" to either
1015 * "real" or "kludge" if we are operating in either
1016 * real or kludge linemode.
1018 if (lmodetype == REAL_LINEMODE)
1019 setenv("LINEMODE", "real", 1);
1020 # ifdef KLUDGELINEMODE
1021 else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK)
1022 setenv("LINEMODE", "kludge", 1);
1027 * Are we working as the bftp daemon? If so, then ask login
1028 * to start bftp instead of shell.
1031 argv = addarg(argv, "-e");
1032 argv = addarg(argv, BFTPPATH);
1035 #ifdef AUTHENTICATION
1036 if (auth_level >= 0 && autologin == AUTH_VALID) {
1037 # if !defined(NO_LOGIN_F)
1038 argv = addarg(argv, "-f");
1039 argv = addarg(argv, "--");
1040 argv = addarg(argv, name);
1042 # if defined(LOGIN_R)
1044 * We don't have support for "login -f", but we
1045 * can fool /bin/login into thinking that we are
1046 * rlogind, and allow us to log in without a
1047 * password. The rlogin protocol expects
1048 * local-user\0remote-user\0term/speed\0
1054 int isecho, israw, xpty, len;
1055 extern int def_rspeed;
1058 * Tell login that we are coming from "localhost".
1059 * If we passed in the real host name, then the
1060 * user would have to allow .rhost access from
1061 * every machine that they want authenticated
1062 * access to work from, which sort of defeats
1063 * the purpose of an authenticated login...
1064 * So, we tell login that the session is coming
1065 * from "localhost", and the user will only have
1066 * to have "localhost" in their .rhost file.
1068 # define LOGIN_HOST "localhost"
1070 argv = addarg(argv, "-r");
1071 argv = addarg(argv, LOGIN_HOST);
1076 isecho = tty_isecho();
1077 israw = tty_israw();
1078 if (isecho || !israw) {
1079 tty_setecho(0); /* Turn off echo */
1080 tty_setraw(1); /* Turn on raw */
1083 len = strlen(name)+1;
1084 write(xpty, name, len);
1085 write(xpty, name, len);
1086 snprintf(speed, sizeof(speed),
1087 "%s/%d", (cp = getenv("TERM")) ? cp : "",
1088 (def_rspeed > 0) ? def_rspeed : 9600);
1089 len = strlen(speed)+1;
1090 write(xpty, speed, len);
1092 if (isecho || !israw) {
1094 tty_setecho(isecho);
1099 * Write a newline to ensure
1100 * that login will be able to
1103 write(xpty, "\n", 1);
1109 argv = addarg(argv, "--");
1110 argv = addarg(argv, name);
1116 argv = addarg(argv, "--");
1117 argv = addarg(argv, user);
1118 #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1121 for (cpp = environ; *cpp; cpp++)
1122 argv = addarg(argv, *cpp);
1126 #ifdef AUTHENTICATION
1127 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
1131 #endif /* AUTHENTICATION */
1137 if (altlogin == NULL) {
1138 altlogin = _PATH_LOGIN;
1140 execv(altlogin, argv);
1142 syslog(LOG_ERR, "%s: %m", altlogin);
1143 fatalperror(net, altlogin);
1148 addarg(char **argv, const char *val)
1154 * 10 entries, a leading length, and a null
1156 argv = (char **)malloc(sizeof(*argv) * 12);
1158 fatal(net, "failure allocating argument space");
1159 *argv++ = (char *)10;
1162 for (cpp = argv; *cpp; cpp++)
1164 if (cpp == &argv[(long)argv[-1]]) {
1166 *argv = (char *)((long)(*argv) + 10);
1167 argv = (char **)realloc(argv, sizeof(*argv)*((long)(*argv) + 2));
1169 fatal(net, "failure allocating argument space");
1171 cpp = &argv[(long)argv[-1] - 10];
1173 if ((*cpp++ = strdup(val)) == NULL)
1174 fatal(net, "failure allocating argument space");
1182 * We only accept the environment variables listed below.
1187 static const char *rej[] = {
1192 static const char *acc[] = {
1193 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
1205 char ** new_environ;
1208 /* Allocate space for scrubbed environment. */
1209 for (count = 1, cpp = environ; *cpp; count++, cpp++)
1211 if ((new_environ = malloc(count * sizeof(char *))) == NULL) {
1216 for (cpp2 = new_environ, cpp = environ; *cpp; cpp++) {
1219 for(p = rej; *p; p++)
1220 if(strncmp(*cpp, *p, strlen(*p)) == 0) {
1227 for(p = acc; *p; p++)
1228 if(strncmp(*cpp, *p, strlen(*p)) == 0)
1231 if ((*cpp2++ = strdup(*cpp)) == NULL) {
1232 environ = new_environ;
1238 environ = new_environ;
1244 * This is the routine to call when we are all through, to
1245 * clean up anything that needs to be cleaned up.
1249 cleanup(int sig __unused)
1252 (void) shutdown(net, SHUT_RDWR);