sys_term.c revision 1.42 1 1.42 perry /* $NetBSD: sys_term.c,v 1.42 2005/02/06 05:58:21 perry Exp $ */
2 1.8 thorpej
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1989, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.39 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.11 mrg #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.8 thorpej #if 0
35 1.8 thorpej static char sccsid[] = "@(#)sys_term.c 8.4+1 (Berkeley) 5/30/95";
36 1.8 thorpej #else
37 1.42 perry __RCSID("$NetBSD: sys_term.c,v 1.42 2005/02/06 05:58:21 perry Exp $");
38 1.8 thorpej #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #include "telnetd.h"
42 1.1 cgd #include "pathnames.h"
43 1.1 cgd
44 1.11 mrg #include <util.h>
45 1.41 christos #include <vis.h>
46 1.11 mrg
47 1.6 jtk #include <sys/cdefs.h>
48 1.3 cgd
49 1.31 wiz #include <utmp.h>
50 1.1 cgd struct utmp wtmp;
51 1.1 cgd
52 1.1 cgd #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
53 1.1 cgd #define SCMPN(a, b) strncmp(a, b, sizeof(a))
54 1.1 cgd
55 1.1 cgd struct termios termbuf, termbuf2; /* pty control structure */
56 1.1 cgd
57 1.42 perry void getptyslave(void);
58 1.42 perry int cleanopen(char *);
59 1.42 perry char **addarg(char **, char *);
60 1.42 perry void scrub_env(void);
61 1.42 perry int getent(char *, char *);
62 1.42 perry char *getstr(const char *, char **);
63 1.16 aidan #ifdef KRB5
64 1.42 perry extern void kerberos5_cleanup(void);
65 1.16 aidan #endif
66 1.11 mrg
67 1.1 cgd /*
68 1.1 cgd * init_termbuf()
69 1.1 cgd * copy_termbuf(cp)
70 1.1 cgd * set_termbuf()
71 1.1 cgd *
72 1.1 cgd * These three routines are used to get and set the "termbuf" structure
73 1.1 cgd * to and from the kernel. init_termbuf() gets the current settings.
74 1.1 cgd * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
75 1.1 cgd * set_termbuf() writes the structure into the kernel.
76 1.1 cgd */
77 1.1 cgd
78 1.36 itojun void
79 1.42 perry init_termbuf(void)
80 1.1 cgd {
81 1.1 cgd (void) tcgetattr(pty, &termbuf);
82 1.1 cgd termbuf2 = termbuf;
83 1.1 cgd }
84 1.1 cgd
85 1.1 cgd #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
86 1.36 itojun void
87 1.42 perry copy_termbuf(char *cp, int len)
88 1.1 cgd {
89 1.1 cgd if (len > sizeof(termbuf))
90 1.1 cgd len = sizeof(termbuf);
91 1.6 jtk memmove((char *)&termbuf, cp, len);
92 1.1 cgd termbuf2 = termbuf;
93 1.1 cgd }
94 1.1 cgd #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
95 1.1 cgd
96 1.36 itojun void
97 1.42 perry set_termbuf(void)
98 1.1 cgd {
99 1.1 cgd /*
100 1.1 cgd * Only make the necessary changes.
101 1.1 cgd */
102 1.6 jtk if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
103 1.1 cgd (void) tcsetattr(pty, TCSANOW, &termbuf);
104 1.1 cgd }
105 1.1 cgd
106 1.1 cgd
107 1.1 cgd /*
108 1.1 cgd * spcset(func, valp, valpp)
109 1.1 cgd *
110 1.1 cgd * This function takes various special characters (func), and
111 1.1 cgd * sets *valp to the current value of that character, and
112 1.1 cgd * *valpp to point to where in the "termbuf" structure that
113 1.1 cgd * value is kept.
114 1.1 cgd *
115 1.1 cgd * It returns the SLC_ level of support for this function.
116 1.1 cgd */
117 1.1 cgd
118 1.1 cgd
119 1.36 itojun int
120 1.42 perry spcset(int func, cc_t *valp, cc_t **valpp)
121 1.1 cgd {
122 1.1 cgd
123 1.1 cgd #define setval(a, b) *valp = termbuf.c_cc[a]; \
124 1.1 cgd *valpp = &termbuf.c_cc[a]; \
125 1.1 cgd return(b);
126 1.1 cgd #define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT);
127 1.1 cgd
128 1.1 cgd switch(func) {
129 1.1 cgd case SLC_EOF:
130 1.1 cgd setval(VEOF, SLC_VARIABLE);
131 1.1 cgd case SLC_EC:
132 1.1 cgd setval(VERASE, SLC_VARIABLE);
133 1.1 cgd case SLC_EL:
134 1.1 cgd setval(VKILL, SLC_VARIABLE);
135 1.1 cgd case SLC_IP:
136 1.1 cgd setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
137 1.1 cgd case SLC_ABORT:
138 1.1 cgd setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
139 1.1 cgd case SLC_XON:
140 1.1 cgd setval(VSTART, SLC_VARIABLE);
141 1.1 cgd case SLC_XOFF:
142 1.1 cgd setval(VSTOP, SLC_VARIABLE);
143 1.1 cgd case SLC_EW:
144 1.1 cgd setval(VWERASE, SLC_VARIABLE);
145 1.1 cgd case SLC_RP:
146 1.1 cgd setval(VREPRINT, SLC_VARIABLE);
147 1.1 cgd case SLC_LNEXT:
148 1.1 cgd setval(VLNEXT, SLC_VARIABLE);
149 1.1 cgd case SLC_AO:
150 1.1 cgd setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
151 1.1 cgd case SLC_SUSP:
152 1.1 cgd setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
153 1.1 cgd case SLC_FORW1:
154 1.1 cgd setval(VEOL, SLC_VARIABLE);
155 1.1 cgd case SLC_FORW2:
156 1.1 cgd setval(VEOL2, SLC_VARIABLE);
157 1.1 cgd case SLC_AYT:
158 1.1 cgd setval(VSTATUS, SLC_VARIABLE);
159 1.1 cgd
160 1.1 cgd case SLC_BRK:
161 1.1 cgd case SLC_SYNCH:
162 1.1 cgd case SLC_EOR:
163 1.1 cgd defval(0);
164 1.1 cgd
165 1.1 cgd default:
166 1.1 cgd *valp = 0;
167 1.1 cgd *valpp = 0;
168 1.1 cgd return(SLC_NOSUPPORT);
169 1.1 cgd }
170 1.1 cgd }
171 1.1 cgd
172 1.1 cgd
173 1.1 cgd /*
174 1.1 cgd * getpty()
175 1.1 cgd *
176 1.1 cgd * Allocate a pty. As a side effect, the external character
177 1.1 cgd * array "line" contains the name of the slave side.
178 1.1 cgd *
179 1.1 cgd * Returns the file descriptor of the opened pty.
180 1.1 cgd */
181 1.1 cgd #ifndef __GNUC__
182 1.19 christos char *line = NULL16STR;
183 1.1 cgd #else
184 1.19 christos static char Xline[] = NULL16STR;
185 1.1 cgd char *line = Xline;
186 1.1 cgd #endif
187 1.1 cgd
188 1.13 perry
189 1.13 perry static int ptyslavefd; /* for cleanopen() */
190 1.13 perry
191 1.13 perry int
192 1.42 perry getpty(int *ptynum)
193 1.13 perry {
194 1.13 perry int ptyfd;
195 1.13 perry
196 1.13 perry ptyfd = openpty(ptynum, &ptyslavefd, line, NULL, NULL);
197 1.13 perry if (ptyfd == 0)
198 1.13 perry return *ptynum;
199 1.13 perry ptyslavefd = -1;
200 1.13 perry return (-1);
201 1.13 perry }
202 1.1 cgd
203 1.1 cgd #ifdef LINEMODE
204 1.1 cgd /*
205 1.1 cgd * tty_flowmode() Find out if flow control is enabled or disabled.
206 1.1 cgd * tty_linemode() Find out if linemode (external processing) is enabled.
207 1.1 cgd * tty_setlinemod(on) Turn on/off linemode.
208 1.1 cgd * tty_isecho() Find out if echoing is turned on.
209 1.1 cgd * tty_setecho(on) Enable/disable character echoing.
210 1.1 cgd * tty_israw() Find out if terminal is in RAW mode.
211 1.1 cgd * tty_binaryin(on) Turn on/off BINARY on input.
212 1.1 cgd * tty_binaryout(on) Turn on/off BINARY on output.
213 1.1 cgd * tty_isediting() Find out if line editing is enabled.
214 1.1 cgd * tty_istrapsig() Find out if signal trapping is enabled.
215 1.1 cgd * tty_setedit(on) Turn on/off line editing.
216 1.1 cgd * tty_setsig(on) Turn on/off signal trapping.
217 1.1 cgd * tty_issofttab() Find out if tab expansion is enabled.
218 1.1 cgd * tty_setsofttab(on) Turn on/off soft tab expansion.
219 1.1 cgd * tty_islitecho() Find out if typed control chars are echoed literally
220 1.1 cgd * tty_setlitecho() Turn on/off literal echo of control chars
221 1.1 cgd * tty_tspeed(val) Set transmit speed to val.
222 1.1 cgd * tty_rspeed(val) Set receive speed to val.
223 1.1 cgd */
224 1.1 cgd
225 1.1 cgd
226 1.36 itojun int
227 1.42 perry tty_linemode(void)
228 1.1 cgd {
229 1.1 cgd return(termbuf.c_lflag & EXTPROC);
230 1.1 cgd }
231 1.1 cgd
232 1.36 itojun void
233 1.42 perry tty_setlinemode(int on)
234 1.1 cgd {
235 1.1 cgd set_termbuf();
236 1.1 cgd (void) ioctl(pty, TIOCEXT, (char *)&on);
237 1.1 cgd init_termbuf();
238 1.1 cgd }
239 1.3 cgd #endif /* LINEMODE */
240 1.1 cgd
241 1.36 itojun int
242 1.42 perry tty_isecho(void)
243 1.1 cgd {
244 1.1 cgd return (termbuf.c_lflag & ECHO);
245 1.1 cgd }
246 1.3 cgd
247 1.36 itojun int
248 1.42 perry tty_flowmode(void)
249 1.3 cgd {
250 1.3 cgd return((termbuf.c_iflag & IXON) ? 1 : 0);
251 1.3 cgd }
252 1.3 cgd
253 1.36 itojun int
254 1.42 perry tty_restartany(void)
255 1.3 cgd {
256 1.3 cgd return((termbuf.c_iflag & IXANY) ? 1 : 0);
257 1.3 cgd }
258 1.1 cgd
259 1.36 itojun void
260 1.42 perry tty_setecho(int on)
261 1.1 cgd {
262 1.1 cgd if (on)
263 1.1 cgd termbuf.c_lflag |= ECHO;
264 1.1 cgd else
265 1.1 cgd termbuf.c_lflag &= ~ECHO;
266 1.1 cgd }
267 1.1 cgd
268 1.36 itojun int
269 1.42 perry tty_israw(void)
270 1.1 cgd {
271 1.1 cgd return(!(termbuf.c_lflag & ICANON));
272 1.1 cgd }
273 1.3 cgd
274 1.36 itojun void
275 1.42 perry tty_binaryin(int on)
276 1.1 cgd {
277 1.1 cgd if (on) {
278 1.1 cgd termbuf.c_iflag &= ~ISTRIP;
279 1.1 cgd } else {
280 1.1 cgd termbuf.c_iflag |= ISTRIP;
281 1.1 cgd }
282 1.1 cgd }
283 1.1 cgd
284 1.36 itojun void
285 1.42 perry tty_binaryout(int on)
286 1.1 cgd {
287 1.1 cgd if (on) {
288 1.1 cgd termbuf.c_cflag &= ~(CSIZE|PARENB);
289 1.1 cgd termbuf.c_cflag |= CS8;
290 1.1 cgd termbuf.c_oflag &= ~OPOST;
291 1.1 cgd } else {
292 1.1 cgd termbuf.c_cflag &= ~CSIZE;
293 1.1 cgd termbuf.c_cflag |= CS7|PARENB;
294 1.1 cgd termbuf.c_oflag |= OPOST;
295 1.1 cgd }
296 1.1 cgd }
297 1.1 cgd
298 1.36 itojun int
299 1.42 perry tty_isbinaryin(void)
300 1.1 cgd {
301 1.1 cgd return(!(termbuf.c_iflag & ISTRIP));
302 1.1 cgd }
303 1.1 cgd
304 1.36 itojun int
305 1.42 perry tty_isbinaryout(void)
306 1.1 cgd {
307 1.1 cgd return(!(termbuf.c_oflag&OPOST));
308 1.1 cgd }
309 1.1 cgd
310 1.1 cgd #ifdef LINEMODE
311 1.36 itojun int
312 1.42 perry tty_isediting(void)
313 1.1 cgd {
314 1.1 cgd return(termbuf.c_lflag & ICANON);
315 1.1 cgd }
316 1.1 cgd
317 1.36 itojun int
318 1.42 perry tty_istrapsig(void)
319 1.1 cgd {
320 1.1 cgd return(termbuf.c_lflag & ISIG);
321 1.1 cgd }
322 1.1 cgd
323 1.36 itojun void
324 1.42 perry tty_setedit(int on)
325 1.1 cgd {
326 1.1 cgd if (on)
327 1.1 cgd termbuf.c_lflag |= ICANON;
328 1.1 cgd else
329 1.1 cgd termbuf.c_lflag &= ~ICANON;
330 1.1 cgd }
331 1.1 cgd
332 1.36 itojun void
333 1.42 perry tty_setsig(int on)
334 1.1 cgd {
335 1.1 cgd if (on)
336 1.1 cgd termbuf.c_lflag |= ISIG;
337 1.1 cgd else
338 1.1 cgd termbuf.c_lflag &= ~ISIG;
339 1.1 cgd }
340 1.1 cgd #endif /* LINEMODE */
341 1.1 cgd
342 1.36 itojun int
343 1.42 perry tty_issofttab(void)
344 1.1 cgd {
345 1.1 cgd # ifdef OXTABS
346 1.1 cgd return (termbuf.c_oflag & OXTABS);
347 1.1 cgd # endif
348 1.1 cgd # ifdef TABDLY
349 1.1 cgd return ((termbuf.c_oflag & TABDLY) == TAB3);
350 1.1 cgd # endif
351 1.1 cgd }
352 1.1 cgd
353 1.36 itojun void
354 1.42 perry tty_setsofttab(int on)
355 1.1 cgd {
356 1.1 cgd if (on) {
357 1.1 cgd # ifdef OXTABS
358 1.1 cgd termbuf.c_oflag |= OXTABS;
359 1.1 cgd # endif
360 1.1 cgd # ifdef TABDLY
361 1.1 cgd termbuf.c_oflag &= ~TABDLY;
362 1.1 cgd termbuf.c_oflag |= TAB3;
363 1.1 cgd # endif
364 1.1 cgd } else {
365 1.1 cgd # ifdef OXTABS
366 1.1 cgd termbuf.c_oflag &= ~OXTABS;
367 1.1 cgd # endif
368 1.1 cgd # ifdef TABDLY
369 1.1 cgd termbuf.c_oflag &= ~TABDLY;
370 1.1 cgd termbuf.c_oflag |= TAB0;
371 1.1 cgd # endif
372 1.1 cgd }
373 1.1 cgd }
374 1.1 cgd
375 1.36 itojun int
376 1.42 perry tty_islitecho(void)
377 1.1 cgd {
378 1.1 cgd # ifdef ECHOCTL
379 1.1 cgd return (!(termbuf.c_lflag & ECHOCTL));
380 1.1 cgd # endif
381 1.1 cgd # ifdef TCTLECH
382 1.1 cgd return (!(termbuf.c_lflag & TCTLECH));
383 1.1 cgd # endif
384 1.1 cgd # if !defined(ECHOCTL) && !defined(TCTLECH)
385 1.1 cgd return (0); /* assumes ctl chars are echoed '^x' */
386 1.1 cgd # endif
387 1.1 cgd }
388 1.1 cgd
389 1.36 itojun void
390 1.42 perry tty_setlitecho(int on)
391 1.1 cgd {
392 1.1 cgd # ifdef ECHOCTL
393 1.1 cgd if (on)
394 1.1 cgd termbuf.c_lflag &= ~ECHOCTL;
395 1.1 cgd else
396 1.1 cgd termbuf.c_lflag |= ECHOCTL;
397 1.1 cgd # endif
398 1.1 cgd # ifdef TCTLECH
399 1.1 cgd if (on)
400 1.1 cgd termbuf.c_lflag &= ~TCTLECH;
401 1.1 cgd else
402 1.1 cgd termbuf.c_lflag |= TCTLECH;
403 1.1 cgd # endif
404 1.1 cgd }
405 1.1 cgd
406 1.36 itojun int
407 1.42 perry tty_iscrnl(void)
408 1.1 cgd {
409 1.1 cgd return (termbuf.c_iflag & ICRNL);
410 1.1 cgd }
411 1.1 cgd
412 1.36 itojun void
413 1.42 perry tty_tspeed(int val)
414 1.1 cgd {
415 1.6 jtk cfsetospeed(&termbuf, val);
416 1.1 cgd }
417 1.1 cgd
418 1.36 itojun void
419 1.42 perry tty_rspeed(int val)
420 1.1 cgd {
421 1.6 jtk cfsetispeed(&termbuf, val);
422 1.1 cgd }
423 1.1 cgd
424 1.1 cgd
425 1.1 cgd
426 1.1 cgd
427 1.1 cgd /*
428 1.1 cgd * getptyslave()
429 1.1 cgd *
430 1.1 cgd * Open the slave side of the pty, and do any initialization
431 1.1 cgd * that is necessary. The return value is a file descriptor
432 1.1 cgd * for the slave side.
433 1.1 cgd */
434 1.22 christos extern int def_tspeed, def_rspeed;
435 1.22 christos extern int def_row, def_col;
436 1.22 christos
437 1.42 perry void
438 1.42 perry getptyslave(void)
439 1.1 cgd {
440 1.42 perry int t = -1;
441 1.1 cgd
442 1.31 wiz #ifdef LINEMODE
443 1.1 cgd int waslm;
444 1.31 wiz #endif
445 1.1 cgd struct winsize ws;
446 1.1 cgd /*
447 1.1 cgd * Opening the slave side may cause initilization of the
448 1.1 cgd * kernel tty structure. We need remember the state of
449 1.1 cgd * if linemode was turned on
450 1.1 cgd * terminal window size
451 1.1 cgd * terminal speed
452 1.1 cgd * so that we can re-set them if we need to.
453 1.1 cgd */
454 1.31 wiz #ifdef LINEMODE
455 1.1 cgd waslm = tty_linemode();
456 1.31 wiz #endif
457 1.1 cgd
458 1.1 cgd /*
459 1.1 cgd * Make sure that we don't have a controlling tty, and
460 1.1 cgd * that we are the session (process group) leader.
461 1.1 cgd */
462 1.1 cgd t = open(_PATH_TTY, O_RDWR);
463 1.1 cgd if (t >= 0) {
464 1.1 cgd (void) ioctl(t, TIOCNOTTY, (char *)0);
465 1.1 cgd (void) close(t);
466 1.1 cgd }
467 1.1 cgd
468 1.1 cgd
469 1.1 cgd
470 1.1 cgd t = cleanopen(line);
471 1.1 cgd if (t < 0)
472 1.1 cgd fatalperror(net, line);
473 1.1 cgd
474 1.3 cgd
475 1.1 cgd /*
476 1.1 cgd * set up the tty modes as we like them to be.
477 1.1 cgd */
478 1.1 cgd init_termbuf();
479 1.1 cgd if (def_row || def_col) {
480 1.6 jtk memset((char *)&ws, 0, sizeof(ws));
481 1.1 cgd ws.ws_col = def_col;
482 1.1 cgd ws.ws_row = def_row;
483 1.1 cgd (void)ioctl(t, TIOCSWINSZ, (char *)&ws);
484 1.1 cgd }
485 1.1 cgd
486 1.1 cgd /*
487 1.1 cgd * Settings for sgtty based systems
488 1.1 cgd */
489 1.1 cgd
490 1.1 cgd /*
491 1.1 cgd * Settings for all other termios/termio based
492 1.1 cgd * systems, other than 4.4BSD. In 4.4BSD the
493 1.1 cgd * kernel does the initial terminal setup.
494 1.1 cgd */
495 1.1 cgd tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
496 1.1 cgd tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
497 1.31 wiz #ifdef LINEMODE
498 1.1 cgd if (waslm)
499 1.1 cgd tty_setlinemode(1);
500 1.31 wiz #endif /* LINEMODE */
501 1.1 cgd
502 1.1 cgd /*
503 1.1 cgd * Set the tty modes, and make this our controlling tty.
504 1.1 cgd */
505 1.1 cgd set_termbuf();
506 1.1 cgd if (login_tty(t) == -1)
507 1.1 cgd fatalperror(net, "login_tty");
508 1.1 cgd if (net > 2)
509 1.1 cgd (void) close(net);
510 1.3 cgd if (pty > 2) {
511 1.1 cgd (void) close(pty);
512 1.3 cgd pty = -1;
513 1.3 cgd }
514 1.1 cgd }
515 1.1 cgd
516 1.1 cgd /*
517 1.1 cgd * Open the specified slave side of the pty,
518 1.1 cgd * making sure that we have a clean tty.
519 1.1 cgd */
520 1.36 itojun int
521 1.42 perry cleanopen(char *ttyline)
522 1.1 cgd {
523 1.13 perry return ptyslavefd;
524 1.1 cgd }
525 1.1 cgd
526 1.1 cgd /*
527 1.1 cgd * startslave(host)
528 1.1 cgd *
529 1.1 cgd * Given a hostname, do whatever
530 1.1 cgd * is necessary to startup the login process on the slave side of the pty.
531 1.1 cgd */
532 1.1 cgd
533 1.1 cgd /* ARGSUSED */
534 1.36 itojun void
535 1.42 perry startslave(char *host, int autologin, char *autoname)
536 1.1 cgd {
537 1.42 perry int i;
538 1.1 cgd
539 1.36 itojun #ifdef AUTHENTICATION
540 1.1 cgd if (!autoname || !autoname[0])
541 1.1 cgd autologin = 0;
542 1.1 cgd
543 1.1 cgd if (autologin < auth_level) {
544 1.1 cgd fatal(net, "Authorization failed");
545 1.1 cgd exit(1);
546 1.1 cgd }
547 1.1 cgd #endif
548 1.1 cgd
549 1.1 cgd
550 1.1 cgd if ((i = fork()) < 0)
551 1.1 cgd fatalperror(net, "fork");
552 1.1 cgd if (i) {
553 1.1 cgd } else {
554 1.11 mrg getptyslave();
555 1.1 cgd start_login(host, autologin, autoname);
556 1.1 cgd /*NOTREACHED*/
557 1.1 cgd }
558 1.1 cgd }
559 1.1 cgd
560 1.1 cgd char *envinit[3];
561 1.1 cgd
562 1.36 itojun void
563 1.42 perry init_env(void)
564 1.1 cgd {
565 1.1 cgd char **envp;
566 1.1 cgd
567 1.1 cgd envp = envinit;
568 1.11 mrg if ((*envp = getenv("TZ")))
569 1.1 cgd *envp++ -= 3;
570 1.1 cgd *envp = 0;
571 1.1 cgd environ = envinit;
572 1.1 cgd }
573 1.1 cgd
574 1.1 cgd
575 1.1 cgd /*
576 1.1 cgd * start_login(host)
577 1.1 cgd *
578 1.1 cgd * Assuming that we are now running as a child processes, this
579 1.1 cgd * function will turn us into the login process.
580 1.1 cgd */
581 1.22 christos extern char *gettyname;
582 1.1 cgd
583 1.36 itojun void
584 1.42 perry start_login(char *host, int autologin, char *name)
585 1.1 cgd {
586 1.42 perry char **argv;
587 1.9 tls #define TABBUFSIZ 512
588 1.9 tls char defent[TABBUFSIZ];
589 1.9 tls char defstrs[TABBUFSIZ];
590 1.9 tls #undef TABBUFSIZ
591 1.23 itojun const char *loginprog = NULL;
592 1.41 christos extern struct sockaddr_storage from;
593 1.41 christos char buf[sizeof(from) * 4 + 1];
594 1.1 cgd
595 1.6 jtk scrub_env();
596 1.6 jtk
597 1.1 cgd /*
598 1.41 christos * -a : pass on the address of the host.
599 1.1 cgd * -h : pass on name of host.
600 1.41 christos * WARNING: -h and -a are accepted by login
601 1.41 christos * if and only if getuid() == 0.
602 1.1 cgd * -p : don't clobber the environment (so terminal type stays set).
603 1.1 cgd *
604 1.1 cgd * -f : force this login, he has already been authenticated
605 1.1 cgd */
606 1.1 cgd argv = addarg(0, "login");
607 1.3 cgd
608 1.41 christos argv = addarg(argv, "-a");
609 1.41 christos (void)strvisx(buf, (const char *)(const void *)&from, sizeof(from),
610 1.41 christos VIS_WHITE);
611 1.41 christos argv = addarg(argv, buf);
612 1.41 christos
613 1.41 christos argv = addarg(argv, "-h");
614 1.41 christos argv = addarg(argv, host);
615 1.41 christos
616 1.1 cgd argv = addarg(argv, "-p");
617 1.6 jtk #ifdef LINEMODE
618 1.6 jtk /*
619 1.6 jtk * Set the environment variable "LINEMODE" to either
620 1.6 jtk * "real" or "kludge" if we are operating in either
621 1.6 jtk * real or kludge linemode.
622 1.6 jtk */
623 1.6 jtk if (lmodetype == REAL_LINEMODE)
624 1.6 jtk setenv("LINEMODE", "real", 1);
625 1.6 jtk # ifdef KLUDGELINEMODE
626 1.6 jtk else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK)
627 1.6 jtk setenv("LINEMODE", "kludge", 1);
628 1.6 jtk # endif
629 1.6 jtk #endif
630 1.36 itojun #ifdef SECURELOGIN
631 1.1 cgd /*
632 1.1 cgd * don't worry about the -f that might get sent.
633 1.1 cgd * A -s is supposed to override it anyhow.
634 1.1 cgd */
635 1.15 dean if (require_secure_login)
636 1.1 cgd argv = addarg(argv, "-s");
637 1.1 cgd #endif
638 1.36 itojun #ifdef AUTHENTICATION
639 1.1 cgd if (auth_level >= 0 && autologin == AUTH_VALID) {
640 1.1 cgd argv = addarg(argv, "-f");
641 1.5 mycroft argv = addarg(argv, "--");
642 1.3 cgd argv = addarg(argv, name);
643 1.1 cgd } else
644 1.1 cgd #endif
645 1.1 cgd if (getenv("USER")) {
646 1.5 mycroft argv = addarg(argv, "--");
647 1.1 cgd argv = addarg(argv, getenv("USER"));
648 1.3 cgd /*
649 1.3 cgd * Assume that login will set the USER variable
650 1.3 cgd * correctly. For SysV systems, this means that
651 1.3 cgd * USER will no longer be set, just LOGNAME by
652 1.3 cgd * login. (The problem is that if the auto-login
653 1.3 cgd * fails, and the user then specifies a different
654 1.3 cgd * account name, he can get logged in with both
655 1.3 cgd * LOGNAME and USER in his environment, but the
656 1.3 cgd * USER value will be wrong.
657 1.3 cgd */
658 1.3 cgd unsetenv("USER");
659 1.1 cgd }
660 1.9 tls if (getent(defent, gettyname) == 1) {
661 1.9 tls char *cp = defstrs;
662 1.9 tls
663 1.9 tls loginprog = getstr("lo", &cp);
664 1.9 tls }
665 1.9 tls if (loginprog == NULL)
666 1.9 tls loginprog = _PATH_LOGIN;
667 1.1 cgd closelog();
668 1.6 jtk /*
669 1.6 jtk * This sleep(1) is in here so that telnetd can
670 1.6 jtk * finish up with the tty. There's a race condition
671 1.6 jtk * the login banner message gets lost...
672 1.6 jtk */
673 1.6 jtk sleep(1);
674 1.9 tls execv(loginprog, argv);
675 1.1 cgd
676 1.25 wiz syslog(LOG_ERR, "%s: %m", loginprog);
677 1.9 tls fatalperror(net, loginprog);
678 1.1 cgd /*NOTREACHED*/
679 1.1 cgd }
680 1.1 cgd
681 1.42 perry char **
682 1.42 perry addarg(char **argv, char *val)
683 1.1 cgd {
684 1.42 perry char **cpp;
685 1.40 itojun char **nargv;
686 1.1 cgd
687 1.1 cgd if (argv == NULL) {
688 1.1 cgd /*
689 1.1 cgd * 10 entries, a leading length, and a null
690 1.1 cgd */
691 1.1 cgd argv = (char **)malloc(sizeof(*argv) * 12);
692 1.1 cgd if (argv == NULL)
693 1.1 cgd return(NULL);
694 1.1 cgd *argv++ = (char *)10;
695 1.1 cgd *argv = (char *)0;
696 1.1 cgd }
697 1.1 cgd for (cpp = argv; *cpp; cpp++)
698 1.1 cgd ;
699 1.7 jtk if (cpp == &argv[(long)argv[-1]]) {
700 1.1 cgd --argv;
701 1.40 itojun nargv = (char **)realloc(argv,
702 1.40 itojun sizeof(*argv) * ((long)(*argv) + 10 + 2));
703 1.18 tron if (argv == NULL) {
704 1.18 tron fatal(net, "not enough memory");
705 1.18 tron /*NOTREACHED*/
706 1.18 tron }
707 1.40 itojun argv = nargv;
708 1.40 itojun *argv = (char *)((long)(*argv) + 10);
709 1.1 cgd argv++;
710 1.7 jtk cpp = &argv[(long)argv[-1] - 10];
711 1.1 cgd }
712 1.1 cgd *cpp++ = val;
713 1.1 cgd *cpp = 0;
714 1.1 cgd return(argv);
715 1.1 cgd }
716 1.1 cgd
717 1.1 cgd /*
718 1.6 jtk * scrub_env()
719 1.6 jtk *
720 1.20 assar * We only accept the environment variables listed below.
721 1.6 jtk */
722 1.20 assar
723 1.11 mrg void
724 1.42 perry scrub_env(void)
725 1.6 jtk {
726 1.20 assar static const char *reject[] = {
727 1.20 assar "TERMCAP=/",
728 1.20 assar NULL
729 1.20 assar };
730 1.20 assar
731 1.27 wiz static const char *acceptstr[] = {
732 1.20 assar "XAUTH=", "XAUTHORITY=", "DISPLAY=",
733 1.20 assar "TERM=",
734 1.20 assar "EDITOR=",
735 1.20 assar "PAGER=",
736 1.20 assar "LOGNAME=",
737 1.20 assar "POSIXLY_CORRECT=",
738 1.20 assar "TERMCAP=",
739 1.20 assar "PRINTER=",
740 1.20 assar NULL
741 1.20 assar };
742 1.20 assar
743 1.20 assar char **cpp, **cpp2;
744 1.20 assar const char **p;
745 1.6 jtk
746 1.6 jtk for (cpp2 = cpp = environ; *cpp; cpp++) {
747 1.20 assar int reject_it = 0;
748 1.20 assar
749 1.20 assar for(p = reject; *p; p++)
750 1.20 assar if(strncmp(*cpp, *p, strlen(*p)) == 0) {
751 1.20 assar reject_it = 1;
752 1.20 assar break;
753 1.20 assar }
754 1.20 assar if (reject_it)
755 1.20 assar continue;
756 1.20 assar
757 1.27 wiz for(p = acceptstr; *p; p++)
758 1.20 assar if(strncmp(*cpp, *p, strlen(*p)) == 0)
759 1.20 assar break;
760 1.20 assar if(*p != NULL)
761 1.6 jtk *cpp2++ = *cpp;
762 1.6 jtk }
763 1.20 assar *cpp2 = NULL;
764 1.6 jtk }
765 1.6 jtk
766 1.6 jtk /*
767 1.1 cgd * cleanup()
768 1.1 cgd *
769 1.1 cgd * This is the routine to call when we are all through, to
770 1.1 cgd * clean up anything that needs to be cleaned up.
771 1.1 cgd */
772 1.36 itojun /* ARGSUSED */
773 1.36 itojun void
774 1.42 perry cleanup(int sig)
775 1.1 cgd {
776 1.14 tsarna char *p, c;
777 1.1 cgd
778 1.1 cgd p = line + sizeof("/dev/") - 1;
779 1.32 christos #ifdef SUPPORT_UTMP
780 1.1 cgd if (logout(p))
781 1.1 cgd logwtmp(p, "", "");
782 1.32 christos #endif
783 1.32 christos #ifdef SUPPORT_UTMPX
784 1.32 christos if (logoutx(p, 0, DEAD_PROCESS))
785 1.32 christos logwtmpx(p, "", "", 0, DEAD_PROCESS);
786 1.32 christos #endif
787 1.1 cgd (void)chmod(line, 0666);
788 1.1 cgd (void)chown(line, 0, 0);
789 1.14 tsarna c = *p; *p = 'p';
790 1.1 cgd (void)chmod(line, 0666);
791 1.1 cgd (void)chown(line, 0, 0);
792 1.14 tsarna *p = c;
793 1.14 tsarna if (ttyaction(line, "telnetd", "root"))
794 1.14 tsarna syslog(LOG_ERR, "%s: ttyaction failed", line);
795 1.1 cgd (void) shutdown(net, 2);
796 1.1 cgd exit(1);
797 1.1 cgd }
798