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>
48 #include "pathnames.h"
53 #include <libtelnet/auth.h>
56 int cleanopen(char *);
60 extern char **environ;
62 #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
63 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
90 # define cfsetospeed(tp, val) (tp)->sg.sg_ospeed = (val)
91 # define cfsetispeed(tp, val) (tp)->sg.sg_ispeed = (val)
92 # define cfgetospeed(tp) (tp)->sg.sg_ospeed
93 # define cfgetispeed(tp) (tp)->sg.sg_ispeed
94 #else /* USE_TERMIO */
97 # define TCSANOW TCSETS
98 # define TCSADRAIN TCSETSW
99 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
102 # define TCSANOW TCSETA
103 # define TCSADRAIN TCSETAW
104 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
106 # define TCSANOW TIOCSETA
107 # define TCSADRAIN TIOCSETAW
108 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
111 # define tcsetattr(f, a, t) ioctl(f, a, t)
112 # define cfsetospeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
113 (tp)->c_cflag |= (val)
114 # define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)
116 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CIBAUD; \
117 (tp)->c_cflag |= ((val)<<IBSHIFT)
118 # define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
120 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
121 (tp)->c_cflag |= (val)
122 # define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)
124 # endif /* TCSANOW */
125 struct termios termbuf, termbuf2; /* pty control structure */
126 #endif /* USE_TERMIO */
128 #include <sys/types.h>
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 = (cc_t *)0; 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.
386 getpty(int *ptynum __unused)
391 p = posix_openpt(O_RDWR|O_NOCTTY);
395 if (grantpt(p) == -1)
398 if (unlockpt(p) == -1)
405 if (strlcpy(line, pn, sizeof line) >= sizeof line)
413 * tty_flowmode() Find out if flow control is enabled or disabled.
414 * tty_linemode() Find out if linemode (external processing) is enabled.
415 * tty_setlinemod(on) Turn on/off linemode.
416 * tty_isecho() Find out if echoing is turned on.
417 * tty_setecho(on) Enable/disable character echoing.
418 * tty_israw() Find out if terminal is in RAW mode.
419 * tty_binaryin(on) Turn on/off BINARY on input.
420 * tty_binaryout(on) Turn on/off BINARY on output.
421 * tty_isediting() Find out if line editing is enabled.
422 * tty_istrapsig() Find out if signal trapping is enabled.
423 * tty_setedit(on) Turn on/off line editing.
424 * tty_setsig(on) Turn on/off signal trapping.
425 * tty_issofttab() Find out if tab expansion is enabled.
426 * tty_setsofttab(on) Turn on/off soft tab expansion.
427 * tty_islitecho() Find out if typed control chars are echoed literally
428 * tty_setlitecho() Turn on/off literal echo of control chars
429 * tty_tspeed(val) Set transmit speed to val.
430 * tty_rspeed(val) Set receive speed to val.
438 return(termbuf.state & TS_EXTPROC);
440 return(termbuf.c_lflag & EXTPROC);
445 tty_setlinemode(int on)
449 (void) ioctl(pty, TIOCEXT, (char *)&on);
454 termbuf.c_lflag |= EXTPROC;
456 termbuf.c_lflag &= ~EXTPROC;
460 #endif /* LINEMODE */
466 return (termbuf.sg.sg_flags & ECHO);
468 return (termbuf.c_lflag & ECHO);
476 return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0);
478 return((termbuf.c_iflag & IXON) ? 1 : 0);
487 return((termbuf.lflags & DECCTQ) ? 0 : 1);
492 return((termbuf.c_iflag & IXANY) ? 1 : 0);
501 termbuf.sg.sg_flags |= ECHO|CRMOD;
503 termbuf.sg.sg_flags &= ~(ECHO|CRMOD);
506 termbuf.c_lflag |= ECHO;
508 termbuf.c_lflag &= ~ECHO;
516 return(termbuf.sg.sg_flags & RAW);
518 return(!(termbuf.c_lflag & ICANON));
522 #ifdef AUTHENTICATION
523 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
529 termbuf.sg.sg_flags |= RAW;
531 termbuf.sg.sg_flags &= ~RAW;
534 termbuf.c_lflag &= ~ICANON;
536 termbuf.c_lflag |= ICANON;
540 #endif /* AUTHENTICATION */
547 termbuf.lflags |= LPASS8;
549 termbuf.lflags &= ~LPASS8;
552 termbuf.c_iflag &= ~ISTRIP;
554 termbuf.c_iflag |= ISTRIP;
560 tty_binaryout(int on)
564 termbuf.lflags |= LLITOUT;
566 termbuf.lflags &= ~LLITOUT;
569 termbuf.c_cflag &= ~(CSIZE|PARENB);
570 termbuf.c_cflag |= CS8;
571 termbuf.c_oflag &= ~OPOST;
573 termbuf.c_cflag &= ~CSIZE;
574 termbuf.c_cflag |= CS7|PARENB;
575 termbuf.c_oflag |= OPOST;
584 return(termbuf.lflags & LPASS8);
586 return(!(termbuf.c_iflag & ISTRIP));
591 tty_isbinaryout(void)
594 return(termbuf.lflags & LLITOUT);
596 return(!(termbuf.c_oflag&OPOST));
605 return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));
607 return(termbuf.c_lflag & ICANON);
615 return(!(termbuf.sg.sg_flags&RAW));
617 return(termbuf.c_lflag & ISIG);
626 termbuf.sg.sg_flags &= ~CBREAK;
628 termbuf.sg.sg_flags |= CBREAK;
631 termbuf.c_lflag |= ICANON;
633 termbuf.c_lflag &= ~ICANON;
645 termbuf.c_lflag |= ISIG;
647 termbuf.c_lflag &= ~ISIG;
650 #endif /* LINEMODE */
656 return (termbuf.sg.sg_flags & XTABS);
659 return (termbuf.c_oflag & OXTABS);
662 return ((termbuf.c_oflag & TABDLY) == TAB3);
668 tty_setsofttab(int on)
672 termbuf.sg.sg_flags |= XTABS;
674 termbuf.sg.sg_flags &= ~XTABS;
678 termbuf.c_oflag |= OXTABS;
681 termbuf.c_oflag &= ~TABDLY;
682 termbuf.c_oflag |= TAB3;
686 termbuf.c_oflag &= ~OXTABS;
689 termbuf.c_oflag &= ~TABDLY;
690 termbuf.c_oflag |= TAB0;
700 return (!(termbuf.lflags & LCTLECH));
703 return (!(termbuf.c_lflag & ECHOCTL));
706 return (!(termbuf.c_lflag & TCTLECH));
708 # if !defined(ECHOCTL) && !defined(TCTLECH)
709 return (0); /* assumes ctl chars are echoed '^x' */
715 tty_setlitecho(int on)
719 termbuf.lflags &= ~LCTLECH;
721 termbuf.lflags |= LCTLECH;
725 termbuf.c_lflag &= ~ECHOCTL;
727 termbuf.c_lflag |= ECHOCTL;
731 termbuf.c_lflag &= ~TCTLECH;
733 termbuf.c_lflag |= TCTLECH;
742 return (termbuf.sg.sg_flags & CRMOD);
744 return (termbuf.c_iflag & ICRNL);
752 struct termspeeds *tp;
754 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
756 if (tp->speed == -1) /* back up to last valid value */
758 cfsetospeed(&termbuf, tp->value);
759 #else /* DECODE_BAUD */
760 cfsetospeed(&termbuf, val);
761 #endif /* DECODE_BAUD */
768 struct termspeeds *tp;
770 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
772 if (tp->speed == -1) /* back up to last valid value */
774 cfsetispeed(&termbuf, tp->value);
775 #else /* DECODE_BAUD */
776 cfsetispeed(&termbuf, val);
777 #endif /* DECODE_BAUD */
783 * Open the slave side of the pty, and do any initialization
797 extern int def_row, def_col;
799 extern int def_tspeed, def_rspeed;
801 * Opening the slave side may cause initilization of the
802 * kernel tty structure. We need remember the state of
803 * if linemode was turned on
804 * terminal window size
807 * so that we can re-set them if we need to.
810 waslm = tty_linemode();
812 erase = termbuf.c_cc[VERASE];
815 * Make sure that we don't have a controlling tty, and
816 * that we are the session (process group) leader.
819 t = open(_PATH_TTY, O_RDWR);
821 (void) ioctl(t, TIOCNOTTY, (char *)0);
828 fatalperror(net, line);
832 * set up the tty modes as we like them to be.
836 if (def_row || def_col) {
837 memset((char *)&ws, 0, sizeof(ws));
840 (void)ioctl(t, TIOCSWINSZ, (char *)&ws);
845 * Settings for sgtty based systems
848 termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
849 # endif /* USE_TERMIO */
852 * Settings for all other termios/termio based
853 * systems, other than 4.4BSD. In 4.4BSD the
854 * kernel does the initial terminal setup.
856 tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
857 tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
859 termbuf.c_cc[VERASE] = erase;
863 # endif /* LINEMODE */
866 * Set the tty modes, and make this our controlling tty.
869 if (login_tty(t) == -1)
870 fatalperror(net, "login_tty");
873 #ifdef AUTHENTICATION
874 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
876 * Leave the pty open so that we can write out the rlogin
877 * protocol for /bin/login, if the authentication works.
885 #endif /* AUTHENTICATION */
892 * Open the specified slave side of the pty,
893 * making sure that we have a clean tty.
901 * Make sure that other people can't open the
902 * slave side of the connection.
904 (void) chown(li, 0, 0);
905 (void) chmod(li, 0600);
909 t = open(line, O_RDWR|O_NOCTTY);
920 * Given a hostname, do whatever
921 * is necessary to startup the login process on the slave side of the pty.
926 startslave(char *host, int autologin, char *autoname)
930 #ifdef AUTHENTICATION
931 if (!autoname || !autoname[0])
934 if (autologin < auth_level) {
935 fatal(net, "Authorization failed");
941 if ((i = fork()) < 0)
942 fatalperror(net, "fork");
946 start_login(host, autologin, autoname);
957 if ((*envp = getenv("TZ")))
967 * Assuming that we are now running as a child processes, this
968 * function will turn us into the login process.
971 #ifndef AUTHENTICATION
972 #define undef1 __unused
978 start_login(char *host undef1, int autologin undef1, char *name undef1)
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))
1004 argv = addarg(argv, "-h");
1005 argv = addarg(argv, host);
1007 #endif /* AUTHENTICATION */
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);
1115 if (getenv("USER")) {
1116 argv = addarg(argv, "--");
1117 argv = addarg(argv, getenv("USER"));
1118 #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1121 for (cpp = environ; *cpp; cpp++)
1122 argv = addarg(argv, *cpp);
1126 * Assume that login will set the USER variable
1127 * correctly. For SysV systems, this means that
1128 * USER will no longer be set, just LOGNAME by
1129 * login. (The problem is that if the auto-login
1130 * fails, and the user then specifies a different
1131 * account name, he can get logged in with both
1132 * LOGNAME and USER in his environment, but the
1133 * USER value will be wrong.
1137 #ifdef AUTHENTICATION
1138 #if defined(NO_LOGIN_F) && defined(LOGIN_R)
1142 #endif /* AUTHENTICATION */
1145 if (altlogin == NULL) {
1146 altlogin = _PATH_LOGIN;
1148 execv(altlogin, argv);
1150 syslog(LOG_ERR, "%s: %m", altlogin);
1151 fatalperror(net, altlogin);
1156 addarg(char **argv, const char *val)
1162 * 10 entries, a leading length, and a null
1164 argv = (char **)malloc(sizeof(*argv) * 12);
1166 fatal(net, "failure allocating argument space");
1167 *argv++ = (char *)10;
1170 for (cpp = argv; *cpp; cpp++)
1172 if (cpp == &argv[(long)argv[-1]]) {
1174 *argv = (char *)((long)(*argv) + 10);
1175 argv = (char **)realloc(argv, sizeof(*argv)*((long)(*argv) + 2));
1177 fatal(net, "failure allocating argument space");
1179 cpp = &argv[(long)argv[-1] - 10];
1181 if ((*cpp++ = strdup(val)) == NULL)
1182 fatal(net, "failure allocating argument space");
1190 * We only accept the environment variables listed below.
1195 static const char *rej[] = {
1200 static const char *acc[] = {
1201 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
1213 char ** new_environ;
1216 /* Allocate space for scrubbed environment. */
1217 for (count = 1, cpp = environ; *cpp; count++, cpp++)
1219 if ((new_environ = malloc(count * sizeof(char *))) == NULL) {
1224 for (cpp2 = new_environ, cpp = environ; *cpp; cpp++) {
1227 for(p = rej; *p; p++)
1228 if(strncmp(*cpp, *p, strlen(*p)) == 0) {
1235 for(p = acc; *p; p++)
1236 if(strncmp(*cpp, *p, strlen(*p)) == 0)
1239 if ((*cpp2++ = strdup(*cpp)) == NULL) {
1240 environ = new_environ;
1246 environ = new_environ;
1252 * This is the routine to call when we are all through, to
1253 * clean up anything that needs to be cleaned up.
1257 cleanup(int sig __unused)
1260 (void) shutdown(net, SHUT_RDWR);