sys_term.c revision 1.18 1 /* $NetBSD: sys_term.c,v 1.18 1999/12/31 12:42:35 tron Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)sys_term.c 8.4+1 (Berkeley) 5/30/95";
40 #else
41 __RCSID("$NetBSD: sys_term.c,v 1.18 1999/12/31 12:42:35 tron Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include "telnetd.h"
46 #include "pathnames.h"
47
48 #include <util.h>
49
50 #include <sys/cdefs.h>
51 #define P __P
52
53 #if defined(AUTHENTICATION)
54 #include <libtelnet/auth.h>
55 #endif
56
57 #if defined(CRAY) || defined(__hpux)
58 # define PARENT_DOES_UTMP
59 #endif
60
61 #ifdef NEWINIT
62 #include <initreq.h>
63 int utmp_len = MAXHOSTNAMELEN; /* sizeof(init_request.host) */
64 #else /* NEWINIT*/
65 # ifdef UTMPX
66 # include <utmpx.h>
67 struct utmpx wtmp;
68 # else
69 # include <utmp.h>
70 struct utmp wtmp;
71 # endif /* UTMPX */
72
73 int utmp_len = sizeof(wtmp.ut_host);
74 # ifndef PARENT_DOES_UTMP
75 char wtmpf[] = "/usr/adm/wtmp";
76 char utmpf[] = "/etc/utmp";
77 # else /* PARENT_DOES_UTMP */
78 char wtmpf[] = "/etc/wtmp";
79 # endif /* PARENT_DOES_UTMP */
80
81 # ifdef CRAY
82 #include <tmpdir.h>
83 #include <sys/wait.h>
84 # if (UNICOS_LVL == '7.0') || (UNICOS_LVL == '7.1')
85 # define UNICOS7x
86 # endif
87
88 # ifdef UNICOS7x
89 #include <sys/sysv.h>
90 #include <sys/secstat.h>
91 extern int secflag;
92 extern struct sysv sysv;
93 # endif /* UNICOS7x */
94 # endif /* CRAY */
95 #endif /* NEWINIT */
96
97 #ifdef STREAMSPTY
98 #include <sac.h>
99 #include <sys/stropts.h>
100 #endif
101
102 #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
103 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
104
105 #ifdef STREAMS
106 #include <sys/stream.h>
107 #endif
108 #ifdef __hpux
109 #include <sys/resource.h>
110 #include <sys/proc.h>
111 #endif
112 #ifdef t_erase
113 #undef t_erase
114 #undef t_kill
115 #undef t_intrc
116 #undef t_quitc
117 #undef t_startc
118 #undef t_stopc
119 #undef t_eofc
120 #undef t_brkc
121 #undef t_suspc
122 #undef t_dsuspc
123 #undef t_rprntc
124 #undef t_flushc
125 #undef t_werasc
126 #undef t_lnextc
127 #endif
128
129 #if defined(UNICOS5) && defined(CRAY2) && !defined(EXTPROC)
130 # define EXTPROC 0400
131 #endif
132
133 #ifndef USE_TERMIO
134 struct termbuf {
135 struct sgttyb sg;
136 struct tchars tc;
137 struct ltchars ltc;
138 int state;
139 int lflags;
140 } termbuf, termbuf2;
141 # define cfsetospeed(tp, val) (tp)->sg.sg_ospeed = (val)
142 # define cfsetispeed(tp, val) (tp)->sg.sg_ispeed = (val)
143 # define cfgetospeed(tp) (tp)->sg.sg_ospeed
144 # define cfgetispeed(tp) (tp)->sg.sg_ispeed
145 #else /* USE_TERMIO */
146 # ifdef SYSV_TERMIO
147 # define termios termio
148 # endif
149 # ifndef TCSANOW
150 # ifdef TCSETS
151 # define TCSANOW TCSETS
152 # define TCSADRAIN TCSETSW
153 # define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
154 # else
155 # ifdef TCSETA
156 # define TCSANOW TCSETA
157 # define TCSADRAIN TCSETAW
158 # define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
159 # else
160 # define TCSANOW TIOCSETA
161 # define TCSADRAIN TIOCSETAW
162 # define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
163 # endif
164 # endif
165 # define tcsetattr(f, a, t) ioctl(f, a, t)
166 # define cfsetospeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
167 (tp)->c_cflag |= (val)
168 # define cfgetospeed(tp) ((tp)->c_cflag & CBAUD)
169 # ifdef CIBAUD
170 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CIBAUD; \
171 (tp)->c_cflag |= ((val)<<IBSHIFT)
172 # define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT)
173 # else
174 # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CBAUD; \
175 (tp)->c_cflag |= (val)
176 # define cfgetispeed(tp) ((tp)->c_cflag & CBAUD)
177 # endif
178 # endif /* TCSANOW */
179 struct termios termbuf, termbuf2; /* pty control structure */
180 # ifdef STREAMSPTY
181 int ttyfd = -1;
182 # endif
183 #endif /* USE_TERMIO */
184
185 void getptyslave __P((void));
186 int cleanopen __P((char *));
187 void init_env __P((void));
188 char **addarg __P((char **, char *));
189 void scrub_env __P((void));
190 int getent __P((char *, char *));
191 char *getstr __P((char *, char **));
192 #ifdef KRB5
193 extern void kerberos5_cleanup __P((void));
194 #endif
195
196 /*
197 * init_termbuf()
198 * copy_termbuf(cp)
199 * set_termbuf()
200 *
201 * These three routines are used to get and set the "termbuf" structure
202 * to and from the kernel. init_termbuf() gets the current settings.
203 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
204 * set_termbuf() writes the structure into the kernel.
205 */
206
207 void
208 init_termbuf()
209 {
210 #ifndef USE_TERMIO
211 (void) ioctl(pty, TIOCGETP, (char *)&termbuf.sg);
212 (void) ioctl(pty, TIOCGETC, (char *)&termbuf.tc);
213 (void) ioctl(pty, TIOCGLTC, (char *)&termbuf.ltc);
214 # ifdef TIOCGSTATE
215 (void) ioctl(pty, TIOCGSTATE, (char *)&termbuf.state);
216 # endif
217 #else
218 # ifdef STREAMSPTY
219 (void) tcgetattr(ttyfd, &termbuf);
220 # else
221 (void) tcgetattr(pty, &termbuf);
222 # endif
223 #endif
224 termbuf2 = termbuf;
225 }
226
227 #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
228 void
229 copy_termbuf(cp, len)
230 char *cp;
231 int len;
232 {
233 if (len > sizeof(termbuf))
234 len = sizeof(termbuf);
235 memmove((char *)&termbuf, cp, len);
236 termbuf2 = termbuf;
237 }
238 #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
239
240 void
241 set_termbuf()
242 {
243 /*
244 * Only make the necessary changes.
245 */
246 #ifndef USE_TERMIO
247 if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg,
248 sizeof(termbuf.sg)))
249 (void) ioctl(pty, TIOCSETN, (char *)&termbuf.sg);
250 if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc,
251 sizeof(termbuf.tc)))
252 (void) ioctl(pty, TIOCSETC, (char *)&termbuf.tc);
253 if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc,
254 sizeof(termbuf.ltc)))
255 (void) ioctl(pty, TIOCSLTC, (char *)&termbuf.ltc);
256 if (termbuf.lflags != termbuf2.lflags)
257 (void) ioctl(pty, TIOCLSET, (char *)&termbuf.lflags);
258 #else /* USE_TERMIO */
259 if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
260 # ifdef STREAMSPTY
261 (void) tcsetattr(ttyfd, TCSANOW, &termbuf);
262 # else
263 (void) tcsetattr(pty, TCSANOW, &termbuf);
264 # endif
265 # if defined(CRAY2) && defined(UNICOS5)
266 needtermstat = 1;
267 # endif
268 #endif /* USE_TERMIO */
269 }
270
271
272 /*
273 * spcset(func, valp, valpp)
274 *
275 * This function takes various special characters (func), and
276 * sets *valp to the current value of that character, and
277 * *valpp to point to where in the "termbuf" structure that
278 * value is kept.
279 *
280 * It returns the SLC_ level of support for this function.
281 */
282
283 #ifndef USE_TERMIO
284 int
285 spcset(func, valp, valpp)
286 int func;
287 cc_t *valp;
288 cc_t **valpp;
289 {
290 switch(func) {
291 case SLC_EOF:
292 *valp = termbuf.tc.t_eofc;
293 *valpp = (cc_t *)&termbuf.tc.t_eofc;
294 return(SLC_VARIABLE);
295 case SLC_EC:
296 *valp = termbuf.sg.sg_erase;
297 *valpp = (cc_t *)&termbuf.sg.sg_erase;
298 return(SLC_VARIABLE);
299 case SLC_EL:
300 *valp = termbuf.sg.sg_kill;
301 *valpp = (cc_t *)&termbuf.sg.sg_kill;
302 return(SLC_VARIABLE);
303 case SLC_IP:
304 *valp = termbuf.tc.t_intrc;
305 *valpp = (cc_t *)&termbuf.tc.t_intrc;
306 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
307 case SLC_ABORT:
308 *valp = termbuf.tc.t_quitc;
309 *valpp = (cc_t *)&termbuf.tc.t_quitc;
310 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
311 case SLC_XON:
312 *valp = termbuf.tc.t_startc;
313 *valpp = (cc_t *)&termbuf.tc.t_startc;
314 return(SLC_VARIABLE);
315 case SLC_XOFF:
316 *valp = termbuf.tc.t_stopc;
317 *valpp = (cc_t *)&termbuf.tc.t_stopc;
318 return(SLC_VARIABLE);
319 case SLC_AO:
320 *valp = termbuf.ltc.t_flushc;
321 *valpp = (cc_t *)&termbuf.ltc.t_flushc;
322 return(SLC_VARIABLE);
323 case SLC_SUSP:
324 *valp = termbuf.ltc.t_suspc;
325 *valpp = (cc_t *)&termbuf.ltc.t_suspc;
326 return(SLC_VARIABLE);
327 case SLC_EW:
328 *valp = termbuf.ltc.t_werasc;
329 *valpp = (cc_t *)&termbuf.ltc.t_werasc;
330 return(SLC_VARIABLE);
331 case SLC_RP:
332 *valp = termbuf.ltc.t_rprntc;
333 *valpp = (cc_t *)&termbuf.ltc.t_rprntc;
334 return(SLC_VARIABLE);
335 case SLC_LNEXT:
336 *valp = termbuf.ltc.t_lnextc;
337 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
338 return(SLC_VARIABLE);
339 case SLC_FORW1:
340 *valp = termbuf.tc.t_brkc;
341 *valpp = (cc_t *)&termbuf.ltc.t_lnextc;
342 return(SLC_VARIABLE);
343 case SLC_BRK:
344 case SLC_SYNCH:
345 case SLC_AYT:
346 case SLC_EOR:
347 *valp = (cc_t)0;
348 *valpp = (cc_t *)0;
349 return(SLC_DEFAULT);
350 default:
351 *valp = (cc_t)0;
352 *valpp = (cc_t *)0;
353 return(SLC_NOSUPPORT);
354 }
355 }
356
357 #else /* USE_TERMIO */
358
359 int
360 spcset(func, valp, valpp)
361 int func;
362 cc_t *valp;
363 cc_t **valpp;
364 {
365
366 #define setval(a, b) *valp = termbuf.c_cc[a]; \
367 *valpp = &termbuf.c_cc[a]; \
368 return(b);
369 #define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT);
370
371 switch(func) {
372 case SLC_EOF:
373 setval(VEOF, SLC_VARIABLE);
374 case SLC_EC:
375 setval(VERASE, SLC_VARIABLE);
376 case SLC_EL:
377 setval(VKILL, SLC_VARIABLE);
378 case SLC_IP:
379 setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
380 case SLC_ABORT:
381 setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
382 case SLC_XON:
383 #ifdef VSTART
384 setval(VSTART, SLC_VARIABLE);
385 #else
386 defval(0x13);
387 #endif
388 case SLC_XOFF:
389 #ifdef VSTOP
390 setval(VSTOP, SLC_VARIABLE);
391 #else
392 defval(0x11);
393 #endif
394 case SLC_EW:
395 #ifdef VWERASE
396 setval(VWERASE, SLC_VARIABLE);
397 #else
398 defval(0);
399 #endif
400 case SLC_RP:
401 #ifdef VREPRINT
402 setval(VREPRINT, SLC_VARIABLE);
403 #else
404 defval(0);
405 #endif
406 case SLC_LNEXT:
407 #ifdef VLNEXT
408 setval(VLNEXT, SLC_VARIABLE);
409 #else
410 defval(0);
411 #endif
412 case SLC_AO:
413 #if !defined(VDISCARD) && defined(VFLUSHO)
414 # define VDISCARD VFLUSHO
415 #endif
416 #ifdef VDISCARD
417 setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
418 #else
419 defval(0);
420 #endif
421 case SLC_SUSP:
422 #ifdef VSUSP
423 setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
424 #else
425 defval(0);
426 #endif
427 #ifdef VEOL
428 case SLC_FORW1:
429 setval(VEOL, SLC_VARIABLE);
430 #endif
431 #ifdef VEOL2
432 case SLC_FORW2:
433 setval(VEOL2, SLC_VARIABLE);
434 #endif
435 case SLC_AYT:
436 #ifdef VSTATUS
437 setval(VSTATUS, SLC_VARIABLE);
438 #else
439 defval(0);
440 #endif
441
442 case SLC_BRK:
443 case SLC_SYNCH:
444 case SLC_EOR:
445 defval(0);
446
447 default:
448 *valp = 0;
449 *valpp = 0;
450 return(SLC_NOSUPPORT);
451 }
452 }
453 #endif /* USE_TERMIO */
454
455 #ifdef CRAY
456 /*
457 * getnpty()
458 *
459 * Return the number of pty's configured into the system.
460 */
461 int
462 getnpty()
463 {
464 #ifdef _SC_CRAY_NPTY
465 int numptys;
466
467 if ((numptys = sysconf(_SC_CRAY_NPTY)) != -1)
468 return numptys;
469 else
470 #endif /* _SC_CRAY_NPTY */
471 return 128;
472 }
473 #endif /* CRAY */
474
475 #ifndef convex
476 /*
477 * getpty()
478 *
479 * Allocate a pty. As a side effect, the external character
480 * array "line" contains the name of the slave side.
481 *
482 * Returns the file descriptor of the opened pty.
483 */
484 #ifndef __GNUC__
485 char *line = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
486 #else
487 static char Xline[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
488 char *line = Xline;
489 #endif
490 #ifdef CRAY
491 char *myline = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
492 #endif /* CRAY */
493
494 #ifdef OPENPTY_PTY
495
496 static int ptyslavefd; /* for cleanopen() */
497
498 int
499 getpty(ptynum)
500 int *ptynum;
501 {
502 int ptyfd;
503
504 ptyfd = openpty(ptynum, &ptyslavefd, line, NULL, NULL);
505 if (ptyfd == 0)
506 return *ptynum;
507 ptyslavefd = -1;
508 return (-1);
509 }
510 #else /* ! OPENPTY_PTY */
511
512 int
513 getpty(ptynum)
514 int *ptynum;
515 {
516 register int p;
517 #ifdef STREAMSPTY
518 int t;
519 char *ptsname();
520
521 p = open("/dev/ptmx", 2);
522 if (p > 0) {
523 grantpt(p);
524 unlockpt(p);
525 strcpy(line, ptsname(p));
526 return(p);
527 }
528
529 #else /* ! STREAMSPTY */
530 #ifndef CRAY
531 register char *cp, *p1, *p2;
532 register int i;
533 #if defined(sun) && defined(TIOCGPGRP) && BSD < 199207
534 int dummy;
535 #endif
536
537 #ifndef __hpux
538 (void) sprintf(line, "/dev/ptyXX");
539 p1 = &line[8];
540 p2 = &line[9];
541 #else
542 (void) sprintf(line, "/dev/ptym/ptyXX");
543 p1 = &line[13];
544 p2 = &line[14];
545 #endif
546
547 for (cp = "pqrstuvwxyzPQRST"; *cp; cp++) {
548 struct stat stb;
549
550 *p1 = *cp;
551 *p2 = '0';
552 /*
553 * This stat() check is just to keep us from
554 * looping through all 256 combinations if there
555 * aren't that many ptys available.
556 */
557 if (stat(line, &stb) < 0)
558 break;
559 for (i = 0; i < 16; i++) {
560 *p2 = "0123456789abcdef"[i];
561 p = open(line, 2);
562 if (p > 0) {
563 #ifndef __hpux
564 line[5] = 't';
565 #else
566 for (p1 = &line[8]; *p1; p1++)
567 *p1 = *(p1+1);
568 line[9] = 't';
569 #endif
570 chown(line, 0, 0);
571 chmod(line, 0600);
572 #if defined(sun) && defined(TIOCGPGRP) && BSD < 199207
573 if (ioctl(p, TIOCGPGRP, &dummy) == 0
574 || errno != EIO) {
575 chmod(line, 0666);
576 close(p);
577 line[5] = 'p';
578 } else
579 #endif /* defined(sun) && defined(TIOCGPGRP) && BSD < 199207 */
580 return(p);
581 }
582 }
583 }
584 #else /* CRAY */
585 extern lowpty, highpty;
586 struct stat sb;
587
588 for (*ptynum = lowpty; *ptynum <= highpty; (*ptynum)++) {
589 (void) sprintf(myline, "/dev/pty/%03d", *ptynum);
590 p = open(myline, 2);
591 if (p < 0)
592 continue;
593 (void) sprintf(line, "/dev/ttyp%03d", *ptynum);
594 /*
595 * Here are some shenanigans to make sure that there
596 * are no listeners lurking on the line.
597 */
598 if(stat(line, &sb) < 0) {
599 (void) close(p);
600 continue;
601 }
602 if(sb.st_uid || sb.st_gid || sb.st_mode != 0600) {
603 chown(line, 0, 0);
604 chmod(line, 0600);
605 (void)close(p);
606 p = open(myline, 2);
607 if (p < 0)
608 continue;
609 }
610 /*
611 * Now it should be safe...check for accessability.
612 */
613 if (access(line, 6) == 0)
614 return(p);
615 else {
616 /* no tty side to pty so skip it */
617 (void) close(p);
618 }
619 }
620 #endif /* CRAY */
621 #endif /* STREAMSPTY */
622 return(-1);
623 }
624 #endif /* OPENPTY_PTY */
625 #endif /* convex */
626
627 #ifdef LINEMODE
628 /*
629 * tty_flowmode() Find out if flow control is enabled or disabled.
630 * tty_linemode() Find out if linemode (external processing) is enabled.
631 * tty_setlinemod(on) Turn on/off linemode.
632 * tty_isecho() Find out if echoing is turned on.
633 * tty_setecho(on) Enable/disable character echoing.
634 * tty_israw() Find out if terminal is in RAW mode.
635 * tty_binaryin(on) Turn on/off BINARY on input.
636 * tty_binaryout(on) Turn on/off BINARY on output.
637 * tty_isediting() Find out if line editing is enabled.
638 * tty_istrapsig() Find out if signal trapping is enabled.
639 * tty_setedit(on) Turn on/off line editing.
640 * tty_setsig(on) Turn on/off signal trapping.
641 * tty_issofttab() Find out if tab expansion is enabled.
642 * tty_setsofttab(on) Turn on/off soft tab expansion.
643 * tty_islitecho() Find out if typed control chars are echoed literally
644 * tty_setlitecho() Turn on/off literal echo of control chars
645 * tty_tspeed(val) Set transmit speed to val.
646 * tty_rspeed(val) Set receive speed to val.
647 */
648
649 #ifdef convex
650 static int linestate;
651 #endif
652
653 int
654 tty_linemode()
655 {
656 #ifndef convex
657 #ifndef USE_TERMIO
658 return(termbuf.state & TS_EXTPROC);
659 #else
660 return(termbuf.c_lflag & EXTPROC);
661 #endif
662 #else
663 return(linestate);
664 #endif
665 }
666
667 void
668 tty_setlinemode(on)
669 int on;
670 {
671 #ifdef TIOCEXT
672 # ifndef convex
673 set_termbuf();
674 # else
675 linestate = on;
676 # endif
677 (void) ioctl(pty, TIOCEXT, (char *)&on);
678 # ifndef convex
679 init_termbuf();
680 # endif
681 #else /* !TIOCEXT */
682 # ifdef EXTPROC
683 if (on)
684 termbuf.c_lflag |= EXTPROC;
685 else
686 termbuf.c_lflag &= ~EXTPROC;
687 # endif
688 #endif /* TIOCEXT */
689 }
690 #endif /* LINEMODE */
691
692 int
693 tty_isecho()
694 {
695 #ifndef USE_TERMIO
696 return (termbuf.sg.sg_flags & ECHO);
697 #else
698 return (termbuf.c_lflag & ECHO);
699 #endif
700 }
701
702 int
703 tty_flowmode()
704 {
705 #ifndef USE_TERMIO
706 return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0);
707 #else
708 return((termbuf.c_iflag & IXON) ? 1 : 0);
709 #endif
710 }
711
712 int
713 tty_restartany()
714 {
715 #ifndef USE_TERMIO
716 # ifdef DECCTQ
717 return((termbuf.lflags & DECCTQ) ? 0 : 1);
718 # else
719 return(-1);
720 # endif
721 #else
722 return((termbuf.c_iflag & IXANY) ? 1 : 0);
723 #endif
724 }
725
726 void
727 tty_setecho(on)
728 int on;
729 {
730 #ifndef USE_TERMIO
731 if (on)
732 termbuf.sg.sg_flags |= ECHO|CRMOD;
733 else
734 termbuf.sg.sg_flags &= ~(ECHO|CRMOD);
735 #else
736 if (on)
737 termbuf.c_lflag |= ECHO;
738 else
739 termbuf.c_lflag &= ~ECHO;
740 #endif
741 }
742
743 int
744 tty_israw()
745 {
746 #ifndef USE_TERMIO
747 return(termbuf.sg.sg_flags & RAW);
748 #else
749 return(!(termbuf.c_lflag & ICANON));
750 #endif
751 }
752
753 #if defined (AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R)
754 int
755 tty_setraw(on)
756 {
757 # ifndef USE_TERMIO
758 if (on)
759 termbuf.sg.sg_flags |= RAW;
760 else
761 termbuf.sg.sg_flags &= ~RAW;
762 # else
763 if (on)
764 termbuf.c_lflag &= ~ICANON;
765 else
766 termbuf.c_lflag |= ICANON;
767 # endif
768 }
769 #endif
770
771 void
772 tty_binaryin(on)
773 int on;
774 {
775 #ifndef USE_TERMIO
776 if (on)
777 termbuf.lflags |= LPASS8;
778 else
779 termbuf.lflags &= ~LPASS8;
780 #else
781 if (on) {
782 termbuf.c_iflag &= ~ISTRIP;
783 } else {
784 termbuf.c_iflag |= ISTRIP;
785 }
786 #endif
787 }
788
789 void
790 tty_binaryout(on)
791 int on;
792 {
793 #ifndef USE_TERMIO
794 if (on)
795 termbuf.lflags |= LLITOUT;
796 else
797 termbuf.lflags &= ~LLITOUT;
798 #else
799 if (on) {
800 termbuf.c_cflag &= ~(CSIZE|PARENB);
801 termbuf.c_cflag |= CS8;
802 termbuf.c_oflag &= ~OPOST;
803 } else {
804 termbuf.c_cflag &= ~CSIZE;
805 termbuf.c_cflag |= CS7|PARENB;
806 termbuf.c_oflag |= OPOST;
807 }
808 #endif
809 }
810
811 int
812 tty_isbinaryin()
813 {
814 #ifndef USE_TERMIO
815 return(termbuf.lflags & LPASS8);
816 #else
817 return(!(termbuf.c_iflag & ISTRIP));
818 #endif
819 }
820
821 int
822 tty_isbinaryout()
823 {
824 #ifndef USE_TERMIO
825 return(termbuf.lflags & LLITOUT);
826 #else
827 return(!(termbuf.c_oflag&OPOST));
828 #endif
829 }
830
831 #ifdef LINEMODE
832 int
833 tty_isediting()
834 {
835 #ifndef USE_TERMIO
836 return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));
837 #else
838 return(termbuf.c_lflag & ICANON);
839 #endif
840 }
841
842 int
843 tty_istrapsig()
844 {
845 #ifndef USE_TERMIO
846 return(!(termbuf.sg.sg_flags&RAW));
847 #else
848 return(termbuf.c_lflag & ISIG);
849 #endif
850 }
851
852 void
853 tty_setedit(on)
854 int on;
855 {
856 #ifndef USE_TERMIO
857 if (on)
858 termbuf.sg.sg_flags &= ~CBREAK;
859 else
860 termbuf.sg.sg_flags |= CBREAK;
861 #else
862 if (on)
863 termbuf.c_lflag |= ICANON;
864 else
865 termbuf.c_lflag &= ~ICANON;
866 #endif
867 }
868
869 void
870 tty_setsig(on)
871 int on;
872 {
873 #ifndef USE_TERMIO
874 if (on)
875 ;
876 #else
877 if (on)
878 termbuf.c_lflag |= ISIG;
879 else
880 termbuf.c_lflag &= ~ISIG;
881 #endif
882 }
883 #endif /* LINEMODE */
884
885 int
886 tty_issofttab()
887 {
888 #ifndef USE_TERMIO
889 return (termbuf.sg.sg_flags & XTABS);
890 #else
891 # ifdef OXTABS
892 return (termbuf.c_oflag & OXTABS);
893 # endif
894 # ifdef TABDLY
895 return ((termbuf.c_oflag & TABDLY) == TAB3);
896 # endif
897 #endif
898 }
899
900 void
901 tty_setsofttab(on)
902 int on;
903 {
904 #ifndef USE_TERMIO
905 if (on)
906 termbuf.sg.sg_flags |= XTABS;
907 else
908 termbuf.sg.sg_flags &= ~XTABS;
909 #else
910 if (on) {
911 # ifdef OXTABS
912 termbuf.c_oflag |= OXTABS;
913 # endif
914 # ifdef TABDLY
915 termbuf.c_oflag &= ~TABDLY;
916 termbuf.c_oflag |= TAB3;
917 # endif
918 } else {
919 # ifdef OXTABS
920 termbuf.c_oflag &= ~OXTABS;
921 # endif
922 # ifdef TABDLY
923 termbuf.c_oflag &= ~TABDLY;
924 termbuf.c_oflag |= TAB0;
925 # endif
926 }
927 #endif
928 }
929
930 int
931 tty_islitecho()
932 {
933 #ifndef USE_TERMIO
934 return (!(termbuf.lflags & LCTLECH));
935 #else
936 # ifdef ECHOCTL
937 return (!(termbuf.c_lflag & ECHOCTL));
938 # endif
939 # ifdef TCTLECH
940 return (!(termbuf.c_lflag & TCTLECH));
941 # endif
942 # if !defined(ECHOCTL) && !defined(TCTLECH)
943 return (0); /* assumes ctl chars are echoed '^x' */
944 # endif
945 #endif
946 }
947
948 void
949 tty_setlitecho(on)
950 int on;
951 {
952 #ifndef USE_TERMIO
953 if (on)
954 termbuf.lflags &= ~LCTLECH;
955 else
956 termbuf.lflags |= LCTLECH;
957 #else
958 # ifdef ECHOCTL
959 if (on)
960 termbuf.c_lflag &= ~ECHOCTL;
961 else
962 termbuf.c_lflag |= ECHOCTL;
963 # endif
964 # ifdef TCTLECH
965 if (on)
966 termbuf.c_lflag &= ~TCTLECH;
967 else
968 termbuf.c_lflag |= TCTLECH;
969 # endif
970 #endif
971 }
972
973 int
974 tty_iscrnl()
975 {
976 #ifndef USE_TERMIO
977 return (termbuf.sg.sg_flags & CRMOD);
978 #else
979 return (termbuf.c_iflag & ICRNL);
980 #endif
981 }
982
983 /*
984 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD).
985 */
986 #if B4800 != 4800
987 #define DECODE_BAUD
988 #endif
989
990 #ifdef DECODE_BAUD
991
992 /*
993 * A table of available terminal speeds
994 */
995 struct termspeeds {
996 int speed;
997 int value;
998 } termspeeds[] = {
999 { 0, B0 }, { 50, B50 }, { 75, B75 },
1000 { 110, B110 }, { 134, B134 }, { 150, B150 },
1001 { 200, B200 }, { 300, B300 }, { 600, B600 },
1002 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
1003 { 4800, B4800 },
1004 #ifdef B7200
1005 { 7200, B7200 },
1006 #endif
1007 { 9600, B9600 },
1008 #ifdef B14400
1009 { 14400, B14400 },
1010 #endif
1011 #ifdef B19200
1012 { 19200, B19200 },
1013 #endif
1014 #ifdef B28800
1015 { 28800, B28800 },
1016 #endif
1017 #ifdef B38400
1018 { 38400, B38400 },
1019 #endif
1020 #ifdef B57600
1021 { 57600, B57600 },
1022 #endif
1023 #ifdef B115200
1024 { 115200, B115200 },
1025 #endif
1026 #ifdef B230400
1027 { 230400, B230400 },
1028 #endif
1029 { -1, 0 }
1030 };
1031 #endif /* DECODE_BUAD */
1032
1033 void
1034 tty_tspeed(val)
1035 int val;
1036 {
1037 #ifdef DECODE_BAUD
1038 register struct termspeeds *tp;
1039
1040 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
1041 ;
1042 if (tp->speed == -1) /* back up to last valid value */
1043 --tp;
1044 cfsetospeed(&termbuf, tp->value);
1045 #else /* DECODE_BUAD */
1046 cfsetospeed(&termbuf, val);
1047 #endif /* DECODE_BUAD */
1048 }
1049
1050 void
1051 tty_rspeed(val)
1052 int val;
1053 {
1054 #ifdef DECODE_BAUD
1055 register struct termspeeds *tp;
1056
1057 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)
1058 ;
1059 if (tp->speed == -1) /* back up to last valid value */
1060 --tp;
1061 cfsetispeed(&termbuf, tp->value);
1062 #else /* DECODE_BAUD */
1063 cfsetispeed(&termbuf, val);
1064 #endif /* DECODE_BAUD */
1065 }
1066
1067 #if defined(CRAY2) && defined(UNICOS5)
1068 int
1069 tty_isnewmap()
1070 {
1071 return((termbuf.c_oflag & OPOST) && (termbuf.c_oflag & ONLCR) &&
1072 !(termbuf.c_oflag & ONLRET));
1073 }
1074 #endif
1075
1076 #ifdef PARENT_DOES_UTMP
1077 # ifndef NEWINIT
1078 extern struct utmp wtmp;
1079 extern char wtmpf[];
1080 # else /* NEWINIT */
1081 int gotalarm;
1082
1083 /* ARGSUSED */
1084 void
1085 nologinproc(sig)
1086 int sig;
1087 {
1088 gotalarm++;
1089 }
1090 # endif /* NEWINIT */
1091 #endif /* PARENT_DOES_UTMP */
1092
1093 #ifndef NEWINIT
1094 # ifdef PARENT_DOES_UTMP
1095 extern void utmp_sig_init P((void));
1096 extern void utmp_sig_reset P((void));
1097 extern void utmp_sig_wait P((void));
1098 extern void utmp_sig_notify P((int));
1099 # endif /* PARENT_DOES_UTMP */
1100 #endif
1101
1102 /*
1103 * getptyslave()
1104 *
1105 * Open the slave side of the pty, and do any initialization
1106 * that is necessary. The return value is a file descriptor
1107 * for the slave side.
1108 */
1109 void
1110 getptyslave()
1111 {
1112 register int t = -1;
1113
1114 #if !defined(CRAY) || !defined(NEWINIT)
1115 # ifdef LINEMODE
1116 int waslm;
1117 # endif
1118 # ifdef TIOCGWINSZ
1119 struct winsize ws;
1120 extern int def_row, def_col;
1121 # endif
1122 extern int def_tspeed, def_rspeed;
1123 /*
1124 * Opening the slave side may cause initilization of the
1125 * kernel tty structure. We need remember the state of
1126 * if linemode was turned on
1127 * terminal window size
1128 * terminal speed
1129 * so that we can re-set them if we need to.
1130 */
1131 # ifdef LINEMODE
1132 waslm = tty_linemode();
1133 # endif
1134
1135
1136 /*
1137 * Make sure that we don't have a controlling tty, and
1138 * that we are the session (process group) leader.
1139 */
1140 # ifdef TIOCNOTTY
1141 t = open(_PATH_TTY, O_RDWR);
1142 if (t >= 0) {
1143 (void) ioctl(t, TIOCNOTTY, (char *)0);
1144 (void) close(t);
1145 }
1146 # endif
1147
1148
1149 # ifdef PARENT_DOES_UTMP
1150 /*
1151 * Wait for our parent to get the utmp stuff to get done.
1152 */
1153 utmp_sig_wait();
1154 # endif
1155
1156 t = cleanopen(line);
1157 if (t < 0)
1158 fatalperror(net, line);
1159
1160 #ifdef STREAMSPTY
1161 #ifdef USE_TERMIO
1162 ttyfd = t;
1163 #endif
1164 if (ioctl(t, I_PUSH, "ptem") < 0)
1165 fatal(net, "I_PUSH ptem");
1166 if (ioctl(t, I_PUSH, "ldterm") < 0)
1167 fatal(net, "I_PUSH ldterm");
1168 if (ioctl(t, I_PUSH, "ttcompat") < 0)
1169 fatal(net, "I_PUSH ttcompat");
1170 if (ioctl(pty, I_PUSH, "pckt") < 0)
1171 fatal(net, "I_PUSH pckt");
1172 #endif
1173
1174 /*
1175 * set up the tty modes as we like them to be.
1176 */
1177 init_termbuf();
1178 # ifdef TIOCGWINSZ
1179 if (def_row || def_col) {
1180 memset((char *)&ws, 0, sizeof(ws));
1181 ws.ws_col = def_col;
1182 ws.ws_row = def_row;
1183 (void)ioctl(t, TIOCSWINSZ, (char *)&ws);
1184 }
1185 # endif
1186
1187 /*
1188 * Settings for sgtty based systems
1189 */
1190 # ifndef USE_TERMIO
1191 termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;
1192 # endif /* USE_TERMIO */
1193
1194 /*
1195 * Settings for UNICOS (and HPUX)
1196 */
1197 # if defined(CRAY) || defined(__hpux)
1198 termbuf.c_oflag = OPOST|ONLCR|TAB3;
1199 termbuf.c_iflag = IGNPAR|ISTRIP|ICRNL|IXON;
1200 termbuf.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK;
1201 termbuf.c_cflag = EXTB|HUPCL|CS8;
1202 # endif
1203
1204 /*
1205 * Settings for all other termios/termio based
1206 * systems, other than 4.4BSD. In 4.4BSD the
1207 * kernel does the initial terminal setup.
1208 */
1209 # if defined(USE_TERMIO) && !(defined(CRAY) || defined(__hpux)) && (BSD <= 43)
1210 # ifndef OXTABS
1211 # define OXTABS 0
1212 # endif
1213 termbuf.c_lflag |= ECHO;
1214 termbuf.c_oflag |= ONLCR|OXTABS;
1215 termbuf.c_iflag |= ICRNL;
1216 termbuf.c_iflag &= ~IXOFF;
1217 # endif /* defined(USE_TERMIO) && !defined(CRAY) && (BSD <= 43) */
1218 tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
1219 tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
1220 # ifdef LINEMODE
1221 if (waslm)
1222 tty_setlinemode(1);
1223 # endif /* LINEMODE */
1224
1225 /*
1226 * Set the tty modes, and make this our controlling tty.
1227 */
1228 set_termbuf();
1229 if (login_tty(t) == -1)
1230 fatalperror(net, "login_tty");
1231 #endif /* !defined(CRAY) || !defined(NEWINIT) */
1232 if (net > 2)
1233 (void) close(net);
1234 #if defined(AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R)
1235 /*
1236 * Leave the pty open so that we can write out the rlogin
1237 * protocol for /bin/login, if the authentication works.
1238 */
1239 #else
1240 if (pty > 2) {
1241 (void) close(pty);
1242 pty = -1;
1243 }
1244 #endif
1245 }
1246
1247 #if !defined(CRAY) || !defined(NEWINIT)
1248 #ifndef O_NOCTTY
1249 #define O_NOCTTY 0
1250 #endif
1251 /*
1252 * Open the specified slave side of the pty,
1253 * making sure that we have a clean tty.
1254 */
1255 int
1256 cleanopen(line)
1257 char *line;
1258 {
1259 #ifdef OPENPTY_PTY
1260 return ptyslavefd;
1261 #else /* ! OPENPTY_PTY */
1262 register int t;
1263 #ifdef UNICOS7x
1264 struct secstat secbuf;
1265 #endif /* UNICOS7x */
1266
1267 #ifndef STREAMSPTY
1268 /*
1269 * Make sure that other people can't open the
1270 * slave side of the connection.
1271 */
1272 (void) chown(line, 0, 0);
1273 (void) chmod(line, 0600);
1274 #endif
1275
1276 # if !defined(CRAY) && (BSD > 43)
1277 (void) revoke(line);
1278 # endif
1279 #ifdef UNICOS7x
1280 if (secflag) {
1281 if (secstat(line, &secbuf) < 0)
1282 return(-1);
1283 if (setulvl(secbuf.st_slevel) < 0)
1284 return(-1);
1285 if (setucmp(secbuf.st_compart) < 0)
1286 return(-1);
1287 }
1288 #endif /* UNICOS7x */
1289
1290 t = open(line, O_RDWR|O_NOCTTY);
1291
1292 #ifdef UNICOS7x
1293 if (secflag) {
1294 if (setulvl(sysv.sy_minlvl) < 0)
1295 return(-1);
1296 if (setucmp(0) < 0)
1297 return(-1);
1298 }
1299 #endif /* UNICOS7x */
1300
1301 if (t < 0)
1302 return(-1);
1303
1304 /*
1305 * Hangup anybody else using this ttyp, then reopen it for
1306 * ourselves.
1307 */
1308 # if !(defined(CRAY) || defined(__hpux)) && (BSD <= 43) && !defined(STREAMSPTY)
1309 (void) signal(SIGHUP, SIG_IGN);
1310 vhangup();
1311 (void) signal(SIGHUP, SIG_DFL);
1312 t = open(line, O_RDWR|O_NOCTTY);
1313 if (t < 0)
1314 return(-1);
1315 # endif
1316 # if defined(CRAY) && defined(TCVHUP)
1317 {
1318 register int i;
1319 (void) signal(SIGHUP, SIG_IGN);
1320 (void) ioctl(t, TCVHUP, (char *)0);
1321 (void) signal(SIGHUP, SIG_DFL);
1322
1323 #ifdef UNICOS7x
1324 if (secflag) {
1325 if (secstat(line, &secbuf) < 0)
1326 return(-1);
1327 if (setulvl(secbuf.st_slevel) < 0)
1328 return(-1);
1329 if (setucmp(secbuf.st_compart) < 0)
1330 return(-1);
1331 }
1332 #endif /* UNICOS7x */
1333
1334 i = open(line, O_RDWR);
1335
1336 #ifdef UNICOS7x
1337 if (secflag) {
1338 if (setulvl(sysv.sy_minlvl) < 0)
1339 return(-1);
1340 if (setucmp(0) < 0)
1341 return(-1);
1342 }
1343 #endif /* UNICOS7x */
1344
1345 if (i < 0)
1346 return(-1);
1347 (void) close(t);
1348 t = i;
1349 }
1350 # endif /* defined(CRAY) && defined(TCVHUP) */
1351 return(t);
1352 #endif /* OPENPTY_PTY */
1353 }
1354 #endif /* !defined(CRAY) || !defined(NEWINIT) */
1355
1356 #if BSD <= 43
1357
1358 int
1359 login_tty(t)
1360 int t;
1361 {
1362 if (setsid() < 0) {
1363 #ifdef ultrix
1364 /*
1365 * The setsid() may have failed because we
1366 * already have a pgrp == pid. Zero out
1367 * our pgrp and try again...
1368 */
1369 if ((setpgrp(0, 0) < 0) || (setsid() < 0))
1370 #endif
1371 fatalperror(net, "setsid()");
1372 }
1373 # ifdef TIOCSCTTY
1374 if (ioctl(t, TIOCSCTTY, (char *)0) < 0)
1375 fatalperror(net, "ioctl(sctty)");
1376 # if defined(CRAY)
1377 /*
1378 * Close the hard fd to /dev/ttypXXX, and re-open through
1379 * the indirect /dev/tty interface.
1380 */
1381 close(t);
1382 if ((t = open("/dev/tty", O_RDWR)) < 0)
1383 fatalperror(net, "open(/dev/tty)");
1384 # endif
1385 # else
1386 /*
1387 * We get our controlling tty assigned as a side-effect
1388 * of opening up a tty device. But on BSD based systems,
1389 * this only happens if our process group is zero. The
1390 * setsid() call above may have set our pgrp, so clear
1391 * it out before opening the tty...
1392 */
1393 # ifndef SOLARIS
1394 (void) setpgrp(0, 0);
1395 # else
1396 (void) setpgrp();
1397 # endif
1398 close(open(line, O_RDWR));
1399 # endif
1400 if (t != 0)
1401 (void) dup2(t, 0);
1402 if (t != 1)
1403 (void) dup2(t, 1);
1404 if (t != 2)
1405 (void) dup2(t, 2);
1406 if (t > 2)
1407 close(t);
1408 return(0);
1409 }
1410 #endif /* BSD <= 43 */
1411
1412 #ifdef NEWINIT
1413 char *gen_id = "fe";
1414 #endif
1415
1416 /*
1417 * startslave(host)
1418 *
1419 * Given a hostname, do whatever
1420 * is necessary to startup the login process on the slave side of the pty.
1421 */
1422
1423 /* ARGSUSED */
1424 void
1425 startslave(host, autologin, autoname)
1426 char *host;
1427 int autologin;
1428 char *autoname;
1429 {
1430 register int i;
1431 #ifdef NEWINIT
1432 extern char *ptyip;
1433 struct init_request request;
1434 void nologinproc();
1435 register int n;
1436 #endif /* NEWINIT */
1437
1438 #if defined(AUTHENTICATION)
1439 if (!autoname || !autoname[0])
1440 autologin = 0;
1441
1442 if (autologin < auth_level) {
1443 fatal(net, "Authorization failed");
1444 exit(1);
1445 }
1446 #endif
1447
1448 #ifndef NEWINIT
1449 # ifdef PARENT_DOES_UTMP
1450 utmp_sig_init();
1451 # endif /* PARENT_DOES_UTMP */
1452
1453 if ((i = fork()) < 0)
1454 fatalperror(net, "fork");
1455 if (i) {
1456 # ifdef PARENT_DOES_UTMP
1457 /*
1458 * Cray parent will create utmp entry for child and send
1459 * signal to child to tell when done. Child waits for signal
1460 * before doing anything important.
1461 */
1462 register int pid = i;
1463 void sigjob P((int));
1464
1465 setpgrp();
1466 utmp_sig_reset(); /* reset handler to default */
1467 /*
1468 * Create utmp entry for child
1469 */
1470 (void) time(&wtmp.ut_time);
1471 wtmp.ut_type = LOGIN_PROCESS;
1472 wtmp.ut_pid = pid;
1473 SCPYN(wtmp.ut_user, "LOGIN");
1474 SCPYN(wtmp.ut_host, host);
1475 SCPYN(wtmp.ut_line, line + sizeof("/dev/") - 1);
1476 #ifndef __hpux
1477 SCPYN(wtmp.ut_id, wtmp.ut_line+3);
1478 #else
1479 SCPYN(wtmp.ut_id, wtmp.ut_line+7);
1480 #endif
1481 pututline(&wtmp);
1482 endutent();
1483 if ((i = open(wtmpf, O_WRONLY|O_APPEND)) >= 0) {
1484 (void) write(i, (char *)&wtmp, sizeof(struct utmp));
1485 (void) close(i);
1486 }
1487 #ifdef CRAY
1488 (void) signal(WJSIGNAL, sigjob);
1489 #endif
1490 utmp_sig_notify(pid);
1491 # endif /* PARENT_DOES_UTMP */
1492 } else {
1493 getptyslave();
1494 start_login(host, autologin, autoname);
1495 /*NOTREACHED*/
1496 }
1497 #else /* NEWINIT */
1498
1499 /*
1500 * Init will start up login process if we ask nicely. We only wait
1501 * for it to start up and begin normal telnet operation.
1502 */
1503 if ((i = open(INIT_FIFO, O_WRONLY)) < 0) {
1504 char tbuf[128];
1505
1506 (void)snprintf(tbuf, sizeof tbuf, "Can't open %s\n", INIT_FIFO);
1507 fatalperror(net, tbuf);
1508 }
1509 memset((char *)&request, 0, sizeof(request));
1510 request.magic = INIT_MAGIC;
1511 SCPYN(request.gen_id, gen_id);
1512 SCPYN(request.tty_id, &line[8]);
1513 SCPYN(request.host, host);
1514 SCPYN(request.term_type, terminaltype ? terminaltype : "network");
1515 #if !defined(UNICOS5)
1516 request.signal = SIGCLD;
1517 request.pid = getpid();
1518 #endif
1519 #ifdef BFTPDAEMON
1520 /*
1521 * Are we working as the bftp daemon?
1522 */
1523 if (bftpd) {
1524 SCPYN(request.exec_name, BFTPPATH);
1525 }
1526 #endif /* BFTPDAEMON */
1527 if (write(i, (char *)&request, sizeof(request)) < 0) {
1528 char tbuf[128];
1529
1530 (void)snprintf(tbuf, sizeof tbuf, "Can't write to %s\n", INIT_FIFO);
1531 fatalperror(net, tbuf);
1532 }
1533 (void) close(i);
1534 (void) signal(SIGALRM, nologinproc);
1535 for (i = 0; ; i++) {
1536 char tbuf[128];
1537
1538 alarm(15);
1539 n = read(pty, ptyip, BUFSIZ);
1540 if (i == 3 || n >= 0 || !gotalarm)
1541 break;
1542 gotalarm = 0;
1543 (void)snprintf(tbuf, sizeof tbuf,
1544 "telnetd: waiting for /etc/init to start login process on %s\r\n", line);
1545 (void)write(net, tbuf, strlen(tbuf));
1546 }
1547 if (n < 0 && gotalarm)
1548 fatal(net, "/etc/init didn't start login process");
1549 pcc += n;
1550 alarm(0);
1551 (void) signal(SIGALRM, SIG_DFL);
1552
1553 return;
1554 #endif /* NEWINIT */
1555 }
1556
1557 char *envinit[3];
1558 extern char **environ;
1559
1560 void
1561 init_env()
1562 {
1563 char **envp;
1564
1565 envp = envinit;
1566 if ((*envp = getenv("TZ")))
1567 *envp++ -= 3;
1568 #if defined(CRAY) || defined(__hpux)
1569 else
1570 *envp++ = "TZ=GMT0";
1571 #endif
1572 *envp = 0;
1573 environ = envinit;
1574 }
1575
1576 #ifndef NEWINIT
1577
1578 /*
1579 * start_login(host)
1580 *
1581 * Assuming that we are now running as a child processes, this
1582 * function will turn us into the login process.
1583 */
1584
1585 void
1586 start_login(host, autologin, name)
1587 char *host;
1588 int autologin;
1589 char *name;
1590 {
1591 register char **argv;
1592 extern char *gettyname;
1593 #define TABBUFSIZ 512
1594 char defent[TABBUFSIZ];
1595 char defstrs[TABBUFSIZ];
1596 #undef TABBUFSIZ
1597 char *loginprog = NULL;
1598 #ifdef UTMPX
1599 register int pid = getpid();
1600 struct utmpx utmpx;
1601 #endif
1602 #ifdef SOLARIS
1603 char *term;
1604 char termbuf[64];
1605 #endif
1606
1607 #ifdef UTMPX
1608 /*
1609 * Create utmp entry for child
1610 */
1611
1612 memset(&utmpx, 0, sizeof(utmpx));
1613 SCPYN(utmpx.ut_user, ".telnet");
1614 SCPYN(utmpx.ut_line, line + sizeof("/dev/") - 1);
1615 utmpx.ut_pid = pid;
1616 utmpx.ut_id[0] = 't';
1617 utmpx.ut_id[1] = 'n';
1618 utmpx.ut_id[2] = SC_WILDC;
1619 utmpx.ut_id[3] = SC_WILDC;
1620 utmpx.ut_type = LOGIN_PROCESS;
1621 (void) time(&utmpx.ut_tv.tv_sec);
1622 if (makeutx(&utmpx) == NULL)
1623 fatal(net, "makeutx failed");
1624 #endif
1625
1626 scrub_env();
1627
1628 /*
1629 * -h : pass on name of host.
1630 * WARNING: -h is accepted by login if and only if
1631 * getuid() == 0.
1632 * -p : don't clobber the environment (so terminal type stays set).
1633 *
1634 * -f : force this login, he has already been authenticated
1635 */
1636 argv = addarg(0, "login");
1637
1638 #if !defined(NO_LOGIN_H)
1639
1640 # if defined (AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R)
1641 /*
1642 * Don't add the "-h host" option if we are going
1643 * to be adding the "-r host" option down below...
1644 */
1645 if ((auth_level < 0) || (autologin != AUTH_VALID))
1646 # endif
1647 {
1648 argv = addarg(argv, "-h");
1649 argv = addarg(argv, host);
1650 #ifdef SOLARIS
1651 /*
1652 * SVR4 version of -h takes TERM= as second arg, or -
1653 */
1654 term = getenv("TERM");
1655 if (term == NULL || term[0] == 0) {
1656 term = "-";
1657 } else {
1658 strcpy(termbuf, "TERM=");
1659 strncat(termbuf, term, sizeof(termbuf) - 6);
1660 term = termbuf;
1661 }
1662 argv = addarg(argv, term);
1663 #endif
1664 }
1665 #endif
1666 #if !defined(NO_LOGIN_P)
1667 argv = addarg(argv, "-p");
1668 #endif
1669 #ifdef LINEMODE
1670 /*
1671 * Set the environment variable "LINEMODE" to either
1672 * "real" or "kludge" if we are operating in either
1673 * real or kludge linemode.
1674 */
1675 if (lmodetype == REAL_LINEMODE)
1676 setenv("LINEMODE", "real", 1);
1677 # ifdef KLUDGELINEMODE
1678 else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK)
1679 setenv("LINEMODE", "kludge", 1);
1680 # endif
1681 #endif
1682 #ifdef BFTPDAEMON
1683 /*
1684 * Are we working as the bftp daemon? If so, then ask login
1685 * to start bftp instead of shell.
1686 */
1687 if (bftpd) {
1688 argv = addarg(argv, "-e");
1689 argv = addarg(argv, BFTPPATH);
1690 } else
1691 #endif
1692 #if defined (SECURELOGIN)
1693 /*
1694 * don't worry about the -f that might get sent.
1695 * A -s is supposed to override it anyhow.
1696 */
1697 if (require_secure_login)
1698 argv = addarg(argv, "-s");
1699 #endif
1700 #if defined (AUTHENTICATION)
1701 if (auth_level >= 0 && autologin == AUTH_VALID) {
1702 # if !defined(NO_LOGIN_F)
1703 # if defined(FORWARD)
1704 if (got_forwarded_creds)
1705 argv = addarg(argv, "-F");
1706 else
1707 # endif /* FORWARD */
1708 argv = addarg(argv, "-f");
1709 argv = addarg(argv, "--");
1710 argv = addarg(argv, name);
1711 # else
1712 # if defined(LOGIN_R)
1713 /*
1714 * We don't have support for "login -f", but we
1715 * can fool /bin/login into thinking that we are
1716 * rlogind, and allow us to log in without a
1717 * password. The rlogin protocol expects
1718 * local-user\0remote-user\0term/speed\0
1719 */
1720
1721 if (pty > 2) {
1722 register char *cp;
1723 char speed[128];
1724 int isecho, israw, xpty, len;
1725 extern int def_rspeed;
1726 # ifndef LOGIN_HOST
1727 /*
1728 * Tell login that we are coming from "localhost".
1729 * If we passed in the real host name, then the
1730 * user would have to allow .rhost access from
1731 * every machine that they want authenticated
1732 * access to work from, which sort of defeats
1733 * the purpose of an authenticated login...
1734 * So, we tell login that the session is coming
1735 * from "localhost", and the user will only have
1736 * to have "localhost" in their .rhost file.
1737 */
1738 # define LOGIN_HOST "localhost"
1739 # endif
1740 argv = addarg(argv, "-r");
1741 argv = addarg(argv, LOGIN_HOST);
1742
1743 xpty = pty;
1744 # ifndef STREAMSPTY
1745 pty = 0;
1746 # else
1747 ttyfd = 0;
1748 # endif
1749 init_termbuf();
1750 isecho = tty_isecho();
1751 israw = tty_israw();
1752 if (isecho || !israw) {
1753 tty_setecho(0); /* Turn off echo */
1754 tty_setraw(1); /* Turn on raw */
1755 set_termbuf();
1756 }
1757 len = strlen(name)+1;
1758 write(xpty, name, len);
1759 write(xpty, name, len);
1760 sprintf(speed, "%s/%d", (cp = getenv("TERM")) ? cp : "",
1761 (def_rspeed > 0) ? def_rspeed : 9600);
1762 len = strlen(speed)+1;
1763 write(xpty, speed, len);
1764
1765 if (isecho || !israw) {
1766 init_termbuf();
1767 tty_setecho(isecho);
1768 tty_setraw(israw);
1769 set_termbuf();
1770 if (!israw) {
1771 /*
1772 * Write a newline to ensure
1773 * that login will be able to
1774 * read the line...
1775 */
1776 write(xpty, "\n", 1);
1777 }
1778 }
1779 pty = xpty;
1780 }
1781 # else
1782 argv = addarg(argv, "--");
1783 argv = addarg(argv, name);
1784 # endif
1785 # endif
1786 } else
1787 #endif
1788 if (getenv("USER")) {
1789 argv = addarg(argv, "--");
1790 argv = addarg(argv, getenv("USER"));
1791 #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1792 {
1793 register char **cpp;
1794 for (cpp = environ; *cpp; cpp++)
1795 argv = addarg(argv, *cpp);
1796 }
1797 #endif
1798 /*
1799 * Assume that login will set the USER variable
1800 * correctly. For SysV systems, this means that
1801 * USER will no longer be set, just LOGNAME by
1802 * login. (The problem is that if the auto-login
1803 * fails, and the user then specifies a different
1804 * account name, he can get logged in with both
1805 * LOGNAME and USER in his environment, but the
1806 * USER value will be wrong.
1807 */
1808 unsetenv("USER");
1809 }
1810 #ifdef SOLARIS
1811 else {
1812 char **p;
1813
1814 argv = addarg(argv, ""); /* no login name */
1815 for (p = environ; *p; p++) {
1816 argv = addarg(argv, *p);
1817 }
1818 }
1819 #endif /* SOLARIS */
1820 #if defined(AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R)
1821 if (pty > 2)
1822 close(pty);
1823 #endif
1824 if (getent(defent, gettyname) == 1) {
1825 char *cp = defstrs;
1826
1827 loginprog = getstr("lo", &cp);
1828 }
1829 if (loginprog == NULL)
1830 loginprog = _PATH_LOGIN;
1831 closelog();
1832 /*
1833 * This sleep(1) is in here so that telnetd can
1834 * finish up with the tty. There's a race condition
1835 * the login banner message gets lost...
1836 */
1837 sleep(1);
1838 execv(loginprog, argv);
1839
1840 syslog(LOG_ERR, "%s: %m\n", loginprog);
1841 fatalperror(net, loginprog);
1842 /*NOTREACHED*/
1843 }
1844
1845 char **
1846 addarg(argv, val)
1847 register char **argv;
1848 register char *val;
1849 {
1850 register char **cpp;
1851
1852 if (argv == NULL) {
1853 /*
1854 * 10 entries, a leading length, and a null
1855 */
1856 argv = (char **)malloc(sizeof(*argv) * 12);
1857 if (argv == NULL)
1858 return(NULL);
1859 *argv++ = (char *)10;
1860 *argv = (char *)0;
1861 }
1862 for (cpp = argv; *cpp; cpp++)
1863 ;
1864 if (cpp == &argv[(long)argv[-1]]) {
1865 --argv;
1866 *argv = (char *)((long)(*argv) + 10);
1867 argv = (char **)realloc(argv, sizeof(*argv)*((long)(*argv) + 2));
1868 if (argv == NULL) {
1869 fatal(net, "not enough memory");
1870 /*NOTREACHED*/
1871 }
1872 argv++;
1873 cpp = &argv[(long)argv[-1] - 10];
1874 }
1875 *cpp++ = val;
1876 *cpp = 0;
1877 return(argv);
1878 }
1879 #endif /* NEWINIT */
1880
1881 /*
1882 * scrub_env()
1883 *
1884 * Remove a few things from the environment that
1885 * don't need to be there.
1886 */
1887 void
1888 scrub_env()
1889 {
1890 register char **cpp, **cpp2;
1891
1892 for (cpp2 = cpp = environ; *cpp; cpp++) {
1893 if (strncmp(*cpp, "LD_", 3) &&
1894 strncmp(*cpp, "_RLD_", 5) &&
1895 strncmp(*cpp, "LIBPATH=", 8) &&
1896 strncmp(*cpp, "IFS=", 4))
1897 *cpp2++ = *cpp;
1898 }
1899 *cpp2 = 0;
1900 }
1901
1902 /*
1903 * cleanup()
1904 *
1905 * This is the routine to call when we are all through, to
1906 * clean up anything that needs to be cleaned up.
1907 */
1908 /* ARGSUSED */
1909 void
1910 cleanup(sig)
1911 int sig;
1912 {
1913 #ifndef PARENT_DOES_UTMP
1914 # if (BSD > 43) || defined(convex)
1915 char *p, c;
1916
1917 p = line + sizeof("/dev/") - 1;
1918 if (logout(p))
1919 logwtmp(p, "", "");
1920 (void)chmod(line, 0666);
1921 (void)chown(line, 0, 0);
1922 c = *p; *p = 'p';
1923 (void)chmod(line, 0666);
1924 (void)chown(line, 0, 0);
1925 *p = c;
1926 if (ttyaction(line, "telnetd", "root"))
1927 syslog(LOG_ERR, "%s: ttyaction failed", line);
1928 (void) shutdown(net, 2);
1929 exit(1);
1930 # else
1931 void rmut();
1932
1933 rmut();
1934 vhangup(); /* XXX */
1935 (void) shutdown(net, 2);
1936 exit(1);
1937 # endif
1938 #else /* PARENT_DOES_UTMP */
1939 # ifdef NEWINIT
1940 (void) shutdown(net, 2);
1941 exit(1);
1942 # else /* NEWINIT */
1943 # ifdef CRAY
1944 static int incleanup = 0;
1945 register int t;
1946 int child_status; /* status of child process as returned by waitpid */
1947 int flags = WNOHANG|WUNTRACED;
1948
1949 /*
1950 * 1: Pick up the zombie, if we are being called
1951 * as the signal handler.
1952 * 2: If we are a nested cleanup(), return.
1953 * 3: Try to clean up TMPDIR.
1954 * 4: Fill in utmp with shutdown of process.
1955 * 5: Close down the network and pty connections.
1956 * 6: Finish up the TMPDIR cleanup, if needed.
1957 */
1958 if (sig == SIGCHLD) {
1959 while (waitpid(-1, &child_status, flags) > 0)
1960 ; /* VOID */
1961 /* Check if the child process was stopped
1962 * rather than exited. We want cleanup only if
1963 * the child has died.
1964 */
1965 if (WIFSTOPPED(child_status)) {
1966 return;
1967 }
1968 }
1969 t = sigblock(sigmask(SIGCHLD));
1970 if (incleanup) {
1971 sigsetmask(t);
1972 return;
1973 }
1974 incleanup = 1;
1975 sigsetmask(t);
1976 #ifdef UNICOS7x
1977 if (secflag) {
1978 /*
1979 * We need to set ourselves back to a null
1980 * label to clean up.
1981 */
1982
1983 setulvl(sysv.sy_minlvl);
1984 setucmp((long)0);
1985 }
1986 #endif /* UNICOS7x */
1987
1988 t = cleantmp(&wtmp);
1989 setutent(); /* just to make sure */
1990 # endif /* CRAY */
1991 rmut(line);
1992 close(pty);
1993 #ifdef KRB5
1994 kerberos5_cleanup();
1995 #endif
1996 (void) shutdown(net, 2);
1997 # ifdef CRAY
1998 if (t == 0)
1999 cleantmp(&wtmp);
2000 # endif /* CRAY */
2001 exit(1);
2002 # endif /* NEWINT */
2003 #endif /* PARENT_DOES_UTMP */
2004 }
2005
2006 #if defined(PARENT_DOES_UTMP) && !defined(NEWINIT)
2007 /*
2008 * _utmp_sig_rcv
2009 * utmp_sig_init
2010 * utmp_sig_wait
2011 * These three functions are used to coordinate the handling of
2012 * the utmp file between the server and the soon-to-be-login shell.
2013 * The server actually creates the utmp structure, the child calls
2014 * utmp_sig_wait(), until the server calls utmp_sig_notify() and
2015 * signals the future-login shell to proceed.
2016 */
2017 static int caught=0; /* NZ when signal intercepted */
2018 static void (*func)(); /* address of previous handler */
2019
2020 void
2021 _utmp_sig_rcv(sig)
2022 int sig;
2023 {
2024 caught = 1;
2025 (void) signal(SIGUSR1, func);
2026 }
2027
2028 void
2029 utmp_sig_init()
2030 {
2031 /*
2032 * register signal handler for UTMP creation
2033 */
2034 if ((int)(func = signal(SIGUSR1, _utmp_sig_rcv)) == -1)
2035 fatalperror(net, "telnetd/signal");
2036 }
2037
2038 void
2039 utmp_sig_reset()
2040 {
2041 (void) signal(SIGUSR1, func); /* reset handler to default */
2042 }
2043
2044 # ifdef __hpux
2045 # define sigoff() /* do nothing */
2046 # define sigon() /* do nothing */
2047 # endif
2048
2049 void
2050 utmp_sig_wait()
2051 {
2052 /*
2053 * Wait for parent to write our utmp entry.
2054 */
2055 sigoff();
2056 while (caught == 0) {
2057 pause(); /* wait until we get a signal (sigon) */
2058 sigoff(); /* turn off signals while we check caught */
2059 }
2060 sigon(); /* turn on signals again */
2061 }
2062
2063 void
2064 utmp_sig_notify(pid)
2065 {
2066 kill(pid, SIGUSR1);
2067 }
2068
2069 # ifdef CRAY
2070 static int gotsigjob = 0;
2071
2072 /*ARGSUSED*/
2073 void
2074 sigjob(sig)
2075 int sig;
2076 {
2077 register int jid;
2078 register struct jobtemp *jp;
2079
2080 while ((jid = waitjob(NULL)) != -1) {
2081 if (jid == 0) {
2082 return;
2083 }
2084 gotsigjob++;
2085 jobend(jid, NULL, NULL);
2086 }
2087 }
2088
2089 /*
2090 * jid_getutid:
2091 * called by jobend() before calling cleantmp()
2092 * to find the correct $TMPDIR to cleanup.
2093 */
2094
2095 struct utmp *
2096 jid_getutid(jid)
2097 int jid;
2098 {
2099 struct utmp *cur = NULL;
2100
2101 setutent(); /* just to make sure */
2102 while (cur = getutent()) {
2103 if ( (cur->ut_type != NULL) && (jid == cur->ut_jid) ) {
2104 return(cur);
2105 }
2106 }
2107
2108 return(0);
2109 }
2110
2111 /*
2112 * Clean up the TMPDIR that login created.
2113 * The first time this is called we pick up the info
2114 * from the utmp. If the job has already gone away,
2115 * then we'll clean up and be done. If not, then
2116 * when this is called the second time it will wait
2117 * for the signal that the job is done.
2118 */
2119 int
2120 cleantmp(wtp)
2121 register struct utmp *wtp;
2122 {
2123 struct utmp *utp;
2124 static int first = 1;
2125 register int mask, omask, ret;
2126 extern struct utmp *getutid P((const struct utmp *_Id));
2127
2128
2129 mask = sigmask(WJSIGNAL);
2130
2131 if (first == 0) {
2132 omask = sigblock(mask);
2133 while (gotsigjob == 0)
2134 sigpause(omask);
2135 return(1);
2136 }
2137 first = 0;
2138 setutent(); /* just to make sure */
2139
2140 utp = getutid(wtp);
2141 if (utp == 0) {
2142 syslog(LOG_ERR, "Can't get /etc/utmp entry to clean TMPDIR");
2143 return(-1);
2144 }
2145 /*
2146 * Nothing to clean up if the user shell was never started.
2147 */
2148 if (utp->ut_type != USER_PROCESS || utp->ut_jid == 0)
2149 return(1);
2150
2151 /*
2152 * Block the WJSIGNAL while we are in jobend().
2153 */
2154 omask = sigblock(mask);
2155 ret = jobend(utp->ut_jid, utp->ut_tpath, utp->ut_user);
2156 sigsetmask(omask);
2157 return(ret);
2158 }
2159
2160 int
2161 jobend(jid, path, user)
2162 register int jid;
2163 register char *path;
2164 register char *user;
2165 {
2166 static int saved_jid = 0;
2167 static int pty_saved_jid = 0;
2168 static char saved_path[sizeof(wtmp.ut_tpath)+1];
2169 static char saved_user[sizeof(wtmp.ut_user)+1];
2170
2171 /*
2172 * this little piece of code comes into play
2173 * only when ptyreconnect is used to reconnect
2174 * to an previous session.
2175 *
2176 * this is the only time when the
2177 * "saved_jid != jid" code is executed.
2178 */
2179
2180 if ( saved_jid && saved_jid != jid ) {
2181 if (!path) { /* called from signal handler */
2182 pty_saved_jid = jid;
2183 } else {
2184 pty_saved_jid = saved_jid;
2185 }
2186 }
2187
2188 if (path) {
2189 strncpy(saved_path, path, sizeof(wtmp.ut_tpath));
2190 strncpy(saved_user, user, sizeof(wtmp.ut_user));
2191 saved_path[sizeof(saved_path)] = '\0';
2192 saved_user[sizeof(saved_user)] = '\0';
2193 }
2194 if (saved_jid == 0) {
2195 saved_jid = jid;
2196 return(0);
2197 }
2198
2199 /* if the jid has changed, get the correct entry from the utmp file */
2200
2201 if ( saved_jid != jid ) {
2202 struct utmp *utp = NULL;
2203 struct utmp *jid_getutid();
2204
2205 utp = jid_getutid(pty_saved_jid);
2206
2207 if (utp == 0) {
2208 syslog(LOG_ERR, "Can't get /etc/utmp entry to clean TMPDIR");
2209 return(-1);
2210 }
2211
2212 cleantmpdir(jid, utp->ut_tpath, utp->ut_user);
2213 return(1);
2214 }
2215
2216 cleantmpdir(jid, saved_path, saved_user);
2217 return(1);
2218 }
2219
2220 /*
2221 * Fork a child process to clean up the TMPDIR
2222 */
2223 cleantmpdir(jid, tpath, user)
2224 register int jid;
2225 register char *tpath;
2226 register char *user;
2227 {
2228 switch(fork()) {
2229 case -1:
2230 syslog(LOG_ERR, "TMPDIR cleanup(%s): fork() failed: %m\n",
2231 tpath);
2232 break;
2233 case 0:
2234 execl(CLEANTMPCMD, CLEANTMPCMD, user, tpath, 0);
2235 syslog(LOG_ERR, "TMPDIR cleanup(%s): execl(%s) failed: %m\n",
2236 tpath, CLEANTMPCMD);
2237 exit(1);
2238 default:
2239 /*
2240 * Forget about child. We will exit, and
2241 * /etc/init will pick it up.
2242 */
2243 break;
2244 }
2245 }
2246 # endif /* CRAY */
2247 #endif /* defined(PARENT_DOES_UTMP) && !defined(NEWINIT) */
2248
2249 /*
2250 * rmut()
2251 *
2252 * This is the function called by cleanup() to
2253 * remove the utmp entry for this person.
2254 */
2255
2256 #ifdef UTMPX
2257 void
2258 rmut()
2259 {
2260 register f;
2261 int found = 0;
2262 struct utmp *u, *utmp;
2263 int nutmp;
2264 struct stat statbf;
2265
2266 struct utmpx *utxp, utmpx;
2267
2268 /*
2269 * This updates the utmpx and utmp entries and make a wtmp/x entry
2270 */
2271
2272 SCPYN(utmpx.ut_line, line + sizeof("/dev/") - 1);
2273 utxp = getutxline(&utmpx);
2274 if (utxp) {
2275 utxp->ut_type = DEAD_PROCESS;
2276 utxp->ut_exit.e_termination = 0;
2277 utxp->ut_exit.e_exit = 0;
2278 (void) time(&utmpx.ut_tv.tv_sec);
2279 utmpx.ut_tv.tv_usec = 0;
2280 modutx(utxp);
2281 }
2282 endutxent();
2283 } /* end of rmut */
2284 #endif
2285
2286 #if !defined(UTMPX) && !(defined(CRAY) || defined(__hpux)) && BSD <= 43
2287 void
2288 rmut()
2289 {
2290 register f;
2291 int found = 0;
2292 struct utmp *u, *utmp;
2293 int nutmp;
2294 struct stat statbf;
2295
2296 f = open(utmpf, O_RDWR);
2297 if (f >= 0) {
2298 (void) fstat(f, &statbf);
2299 utmp = (struct utmp *)malloc((unsigned)statbf.st_size);
2300 if (!utmp)
2301 syslog(LOG_ERR, "utmp malloc failed");
2302 if (statbf.st_size && utmp) {
2303 nutmp = read(f, (char *)utmp, (int)statbf.st_size);
2304 nutmp /= sizeof(struct utmp);
2305
2306 for (u = utmp ; u < &utmp[nutmp] ; u++) {
2307 if (SCMPN(u->ut_line, line+5) ||
2308 u->ut_name[0]==0)
2309 continue;
2310 (void)lseek(f, (off_t)((long)u)-((long)utmp),
2311 SEEK_SET);
2312 SCPYN(u->ut_name, "");
2313 SCPYN(u->ut_host, "");
2314 (void) time(&u->ut_time);
2315 (void) write(f, (char *)u, sizeof(wtmp));
2316 found++;
2317 }
2318 }
2319 (void) close(f);
2320 }
2321 if (found) {
2322 f = open(wtmpf, O_WRONLY|O_APPEND);
2323 if (f >= 0) {
2324 SCPYN(wtmp.ut_line, line+5);
2325 SCPYN(wtmp.ut_name, "");
2326 SCPYN(wtmp.ut_host, "");
2327 (void) time(&wtmp.ut_time);
2328 (void) write(f, (char *)&wtmp, sizeof(wtmp));
2329 (void) close(f);
2330 }
2331 }
2332 (void) chmod(line, 0666);
2333 (void) chown(line, 0, 0);
2334 line[strlen("/dev/")] = 'p';
2335 (void) chmod(line, 0666);
2336 (void) chown(line, 0, 0);
2337 } /* end of rmut */
2338 #endif /* CRAY */
2339
2340 #ifdef __hpux
2341 rmut (line)
2342 char *line;
2343 {
2344 struct utmp utmp;
2345 struct utmp *utptr;
2346 int fd; /* for /etc/wtmp */
2347
2348 utmp.ut_type = USER_PROCESS;
2349 (void) strncpy(utmp.ut_id, line+12, sizeof(utmp.ut_id));
2350 (void) setutent();
2351 utptr = getutid(&utmp);
2352 /* write it out only if it exists */
2353 if (utptr) {
2354 utptr->ut_type = DEAD_PROCESS;
2355 utptr->ut_time = time((long *) 0);
2356 (void) pututline(utptr);
2357 /* set wtmp entry if wtmp file exists */
2358 if ((fd = open(wtmpf, O_WRONLY | O_APPEND)) >= 0) {
2359 (void) write(fd, utptr, sizeof(utmp));
2360 (void) close(fd);
2361 }
2362 }
2363 (void) endutent();
2364
2365 (void) chmod(line, 0666);
2366 (void) chown(line, 0, 0);
2367 line[14] = line[13];
2368 line[13] = line[12];
2369 line[8] = 'm';
2370 line[9] = '/';
2371 line[10] = 'p';
2372 line[11] = 't';
2373 line[12] = 'y';
2374 (void) chmod(line, 0666);
2375 (void) chown(line, 0, 0);
2376 }
2377 #endif
2378