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