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.
380 getpty(int *ptynum __unused)
385 p = posix_openpt(O_RDWR|O_NOCTTY);
389 if (grantpt(p) == -1)
392 if (unlockpt(p) == -1)
399 if (strlcpy(line, pn, sizeof line) >= sizeof line)
407 * tty_flowmode() Find out if flow control is enabled or disabled.
408 * tty_linemode() Find out if linemode (external processing) is enabled.
409 * tty_setlinemod(on) Turn on/off linemode.
410 * tty_isecho() Find out if echoing is turned on.
411 * tty_setecho(on) Enable/disable character echoing.
412 * tty_israw() Find out if terminal is in RAW mode.
413 * tty_binaryin(on) Turn on/off BINARY on input.
414 * tty_binaryout(on) Turn on/off BINARY on output.
415 * tty_isediting() Find out if line editing is enabled.
416 * tty_istrapsig() Find out if signal trapping is enabled.
417 * tty_setedit(on) Turn on/off line editing.
418 * tty_setsig(on) Turn on/off signal trapping.
419 * tty_issofttab() Find out if tab expansion is enabled.
420 * tty_setsofttab(on) Turn on/off soft tab expansion.
421 * tty_islitecho() Find out if typed control chars are echoed literally
422 * tty_setlitecho() Turn on/off literal echo of control chars
423 * tty_tspeed(val) Set transmit speed to val.
424 * tty_rspeed(val) Set receive speed to val.
432 return(termbuf.state & TS_EXTPROC);
434 return(termbuf.c_lflag & EXTPROC);
439 tty_setlinemode(int on)
443 (void) ioctl(pty, TIOCEXT, (char *)&on);
448 termbuf.c_lflag |= EXTPROC;
450 termbuf.c_lflag &= ~EXTPROC;
454 #endif /* LINEMODE */
460 return (termbuf.sg.sg_flags & ECHO);
462 return (termbuf.c_lflag & ECHO);
470 return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0);
472 return((termbuf.c_iflag & IXON) ? 1 : 0);
481 return((termbuf.lflags & DECCTQ) ? 0 : 1);
486 return((termbuf.c_iflag & IXANY) ? 1 : 0);
495 termbuf.sg.sg_flags |= ECHO|CRMOD;
497 termbuf.sg.sg_flags &= ~(ECHO|CRMOD);
500 termbuf.c_lflag |= ECHO;
502 termbuf.c_lflag &= ~ECHO;
510 return(termbuf.sg.sg_flags & RAW);
512 return(!(termbuf.c_lflag & ICANON));
516 #ifdef AUTHENTICATION
517 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
523 termbuf.sg.sg_flags |= RAW;
525 termbuf.sg.sg_flags &= ~RAW;
528 termbuf.c_lflag &= ~ICANON;
530 termbuf.c_lflag |= ICANON;
534 #endif /* AUTHENTICATION */
541 termbuf.lflags |= LPASS8;
543 termbuf.lflags &= ~LPASS8;
546 termbuf.c_iflag &= ~ISTRIP;
548 termbuf.c_iflag |= ISTRIP;
554 tty_binaryout(int on)
558 termbuf.lflags |= LLITOUT;
560 termbuf.lflags &= ~LLITOUT;
563 termbuf.c_cflag &= ~(CSIZE|PARENB);
564 termbuf.c_cflag |= CS8;
565 termbuf.c_oflag &= ~OPOST;
567 termbuf.c_cflag &= ~CSIZE;
568 termbuf.c_cflag |= CS7|PARENB;
569 termbuf.c_oflag |= OPOST;
578 return(termbuf.lflags & LPASS8);
580 return(!(termbuf.c_iflag & ISTRIP));
585 tty_isbinaryout(void)
588 return(termbuf.lflags & LLITOUT);
590 return(!(termbuf.c_oflag&OPOST));
599 return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));
601 return(termbuf.c_lflag & ICANON);
609 return(!(termbuf.sg.sg_flags&RAW));
611 return(termbuf.c_lflag & ISIG);
620 termbuf.sg.sg_flags &= ~CBREAK;
622 termbuf.sg.sg_flags |= CBREAK;
625 termbuf.c_lflag |= ICANON;
627 termbuf.c_lflag &= ~ICANON;
639 termbuf.c_lflag |= ISIG;
641 termbuf.c_lflag &= ~ISIG;
644 #endif /* LINEMODE */
650 return (termbuf.sg.sg_flags & XTABS);
653 return (termbuf.c_oflag & OXTABS);
656 return ((termbuf.c_oflag & TABDLY) == TAB3);
662 tty_setsofttab(int on)
666 termbuf.sg.sg_flags |= XTABS;
668 termbuf.sg.sg_flags &= ~XTABS;
672 termbuf.c_oflag |= OXTABS;
675 termbuf.c_oflag &= ~TABDLY;
676 termbuf.c_oflag |= TAB3;
680 termbuf.c_oflag &= ~OXTABS;
683 termbuf.c_oflag &= ~TABDLY;
684 termbuf.c_oflag |= TAB0;
694 return (!(termbuf.lflags & LCTLECH));
697 return (!(termbuf.c_lflag & ECHOCTL));
700 return (!(termbuf.c_lflag & TCTLECH));
702 # if !defined(ECHOCTL) && !defined(TCTLECH)
703 return (0); /* assumes ctl chars are echoed '^x' */
709 tty_setlitecho(int on)
713 termbuf.lflags &= ~LCTLECH;
715 termbuf.lflags |= LCTLECH;
719 termbuf.c_lflag &= ~ECHOCTL;
721 termbuf.c_lflag |= ECHOCTL;
725 termbuf.c_lflag &= ~TCTLECH;
727 termbuf.c_lflag |= TCTLECH;
736 return (termbuf.sg.sg_flags & CRMOD);
738 return (termbuf.c_iflag & ICRNL);
746 struct termspeeds *tp;
748 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
750 if (tp->speed == -1) /* back up to last valid value */
752 cfsetospeed(&termbuf, tp->value);
753 #else /* DECODE_BAUD */
754 cfsetospeed(&termbuf, val);
755 #endif /* DECODE_BAUD */
762 struct termspeeds *tp;
764 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
766 if (tp->speed == -1) /* back up to last valid value */
768 cfsetispeed(&termbuf, tp->value);
769 #else /* DECODE_BAUD */
770 cfsetispeed(&termbuf, val);
771 #endif /* DECODE_BAUD */
777 * Open the slave side of the pty, and do any initialization
791 extern int def_row, def_col;
793 extern int def_tspeed, def_rspeed;
795 * Opening the slave side may cause initilization of the
796 * kernel tty structure. We need remember the state of
797 * if linemode was turned on
798 * terminal window size
801 * so that we can re-set them if we need to.
804 waslm = tty_linemode();
806 erase = termbuf.c_cc[VERASE];
809 * Make sure that we don't have a controlling tty, and
810 * that we are the session (process group) leader.
813 t = open(_PATH_TTY, O_RDWR);
815 (void) ioctl(t, TIOCNOTTY, (char *)0);
822 fatalperror(net, line);
826 * set up the tty modes as we like them to be.
830 if (def_row || def_col) {
831 memset((char *)&ws, 0, sizeof(ws));
834 (void)ioctl(t, TIOCSWINSZ, (char *)&ws);
839 * Settings for sgtty based systems
842 termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
843 # endif /* USE_TERMIO */
846 * Settings for all other termios/termio based
847 * systems, other than 4.4BSD. In 4.4BSD the
848 * kernel does the initial terminal setup.
850 tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
851 tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
853 termbuf.c_cc[VERASE] = erase;
857 # endif /* LINEMODE */
860 * Set the tty modes, and make this our controlling tty.
863 if (login_tty(t) == -1)
864 fatalperror(net, "login_tty");
867 #ifdef AUTHENTICATION
868 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
870 * Leave the pty open so that we can write out the rlogin
871 * protocol for /bin/login, if the authentication works.
879 #endif /* AUTHENTICATION */
886 * Open the specified slave side of the pty,
887 * making sure that we have a clean tty.
895 * Make sure that other people can't open the
896 * slave side of the connection.
898 (void) chown(li, 0, 0);
899 (void) chmod(li, 0600);
903 t = open(line, O_RDWR|O_NOCTTY);
914 * Given a hostname, do whatever
915 * is necessary to startup the login process on the slave side of the pty.
920 startslave(char *host, int autologin, char *autoname)
924 #ifdef AUTHENTICATION
925 if (!autoname || !autoname[0])
928 if (autologin < auth_level) {
929 fatal(net, "Authorization failed");
935 if ((i = fork()) < 0)
936 fatalperror(net, "fork");
940 start_login(host, autologin, autoname);
951 if ((*envp = getenv("TZ")))
961 * Assuming that we are now running as a child processes, this
962 * function will turn us into the login process.
965 #ifndef AUTHENTICATION
966 #define undef1 __unused
972 start_login(char *host undef1, int autologin undef1, char *name undef1)
977 user = getenv("USER");
978 user = (user != NULL) ? strdup(user) : NULL;
983 * -h : pass on name of host.
984 * WARNING: -h is accepted by login if and only if
986 * -p : don't clobber the environment (so terminal type stays set).
988 * -f : force this login, he has already been authenticated
990 argv = addarg(0, "login");
992 #if !defined(NO_LOGIN_H)
993 #ifdef AUTHENTICATION
994 # if defined(NO_LOGIN_F) && defined(LOGIN_R)
996 * Don't add the "-h host" option if we are going
997 * to be adding the "-r host" option down below...
999 if ((auth_level < 0) || (autologin != AUTH_VALID))
1001 #endif /* AUTHENTICATION */
1003 argv = addarg(argv, "-h");
1004 argv = addarg(argv, host);
1007 #if !defined(NO_LOGIN_P)
1008 argv = addarg(argv, "-p");
1012 * Set the environment variable "LINEMODE" to either
1013 * "real" or "kludge" if we are operating in either
1014 * real or kludge linemode.
1016 if (lmodetype == REAL_LINEMODE)
1017 setenv("LINEMODE", "real", 1);
1018 # ifdef KLUDGELINEMODE
1019 else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK)
1020 setenv("LINEMODE", "kludge", 1);
1025 * Are we working as the bftp daemon? If so, then ask login
1026 * to start bftp instead of shell.
1029 argv = addarg(argv, "-e");
1030 argv = addarg(argv, BFTPPATH);
1033 #ifdef AUTHENTICATION
1034 if (auth_level >= 0 && autologin == AUTH_VALID) {
1035 # if !defined(NO_LOGIN_F)
1036 argv = addarg(argv, "-f");
1037 argv = addarg(argv, "--");
1038 argv = addarg(argv, name);
1040 # if defined(LOGIN_R)
1042 * We don't have support for "login -f", but we
1043 * can fool /bin/login into thinking that we are
1044 * rlogind, and allow us to log in without a
1045 * password. The rlogin protocol expects
1046 * local-user\0remote-user\0term/speed\0
1052 int isecho, israw, xpty, len;
1053 extern int def_rspeed;
1056 * Tell login that we are coming from "localhost".
1057 * If we passed in the real host name, then the
1058 * user would have to allow .rhost access from
1059 * every machine that they want authenticated
1060 * access to work from, which sort of defeats
1061 * the purpose of an authenticated login...
1062 * So, we tell login that the session is coming
1063 * from "localhost", and the user will only have
1064 * to have "localhost" in their .rhost file.
1066 # define LOGIN_HOST "localhost"
1068 argv = addarg(argv, "-r");
1069 argv = addarg(argv, LOGIN_HOST);
1074 isecho = tty_isecho();
1075 israw = tty_israw();
1076 if (isecho || !israw) {
1077 tty_setecho(0); /* Turn off echo */
1078 tty_setraw(1); /* Turn on raw */
1081 len = strlen(name)+1;
1082 write(xpty, name, len);
1083 write(xpty, name, len);
1084 snprintf(speed, sizeof(speed),
1085 "%s/%d", (cp = getenv("TERM")) ? cp : "",
1086 (def_rspeed > 0) ? def_rspeed : 9600);
1087 len = strlen(speed)+1;
1088 write(xpty, speed, len);
1090 if (isecho || !israw) {
1092 tty_setecho(isecho);
1097 * Write a newline to ensure
1098 * that login will be able to
1101 write(xpty, "\n", 1);
1107 argv = addarg(argv, "--");
1108 argv = addarg(argv, name);
1114 argv = addarg(argv, "--");
1115 argv = addarg(argv, user);
1116 #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1119 for (cpp = environ; *cpp; cpp++)
1120 argv = addarg(argv, *cpp);
1124 #ifdef AUTHENTICATION
1125 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
1129 #endif /* AUTHENTICATION */
1135 if (altlogin == NULL) {
1136 altlogin = _PATH_LOGIN;
1138 execv(altlogin, argv);
1140 syslog(LOG_ERR, "%s: %m", altlogin);
1141 fatalperror(net, altlogin);
1146 addarg(char **argv, const char *val)
1152 * 10 entries, a leading length, and a null
1154 argv = (char **)malloc(sizeof(*argv) * 12);
1156 fatal(net, "failure allocating argument space");
1157 *argv++ = (char *)10;
1160 for (cpp = argv; *cpp; cpp++)
1162 if (cpp == &argv[(long)argv[-1]]) {
1164 *argv = (char *)((long)(*argv) + 10);
1165 argv = (char **)realloc(argv, sizeof(*argv)*((long)(*argv) + 2));
1167 fatal(net, "failure allocating argument space");
1169 cpp = &argv[(long)argv[-1] - 10];
1171 if ((*cpp++ = strdup(val)) == NULL)
1172 fatal(net, "failure allocating argument space");
1180 * We only accept the environment variables listed below.
1185 static const char *rej[] = {
1190 static const char *acc[] = {
1191 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
1203 char ** new_environ;
1206 /* Allocate space for scrubbed environment. */
1207 for (count = 1, cpp = environ; *cpp; count++, cpp++)
1209 if ((new_environ = malloc(count * sizeof(char *))) == NULL) {
1214 for (cpp2 = new_environ, cpp = environ; *cpp; cpp++) {
1217 for(p = rej; *p; p++)
1218 if(strncmp(*cpp, *p, strlen(*p)) == 0) {
1225 for(p = acc; *p; p++)
1226 if(strncmp(*cpp, *p, strlen(*p)) == 0)
1229 if ((*cpp2++ = strdup(*cpp)) == NULL) {
1230 environ = new_environ;
1236 environ = new_environ;
1242 * This is the routine to call when we are all through, to
1243 * clean up anything that needs to be cleaned up.
1247 cleanup(int sig __unused)
1250 (void) shutdown(net, SHUT_RDWR);