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