sys_term.c revision 1.20 1 /* $NetBSD: sys_term.c,v 1.20 2000/12/09 00:50:21 assar 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.20 2000/12/09 00:50:21 assar 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 = NULL16STR;
486 #else
487 static char Xline[] = NULL16STR;
488 char *line = Xline;
489 #endif
490 #ifdef CRAY
491 char *myline = NULL16STR;
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 (void)strlcpy(line, ptsname(p), sizeof(NULL16STR));
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 termnamebuf[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 (void)strcpy(termnamebuf, "TERM=");
1659 (void)strlcpy(&termnamebuf[5], term,
1660 sizeof(termnamebuf) - 6);
1661 term = termnamebuf;
1662 }
1663 argv = addarg(argv, term);
1664 #endif
1665 }
1666 #endif
1667 #if !defined(NO_LOGIN_P)
1668 argv = addarg(argv, "-p");
1669 #endif
1670 #ifdef LINEMODE
1671 /*
1672 * Set the environment variable "LINEMODE" to either
1673 * "real" or "kludge" if we are operating in either
1674 * real or kludge linemode.
1675 */
1676 if (lmodetype == REAL_LINEMODE)
1677 setenv("LINEMODE", "real", 1);
1678 # ifdef KLUDGELINEMODE
1679 else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK)
1680 setenv("LINEMODE", "kludge", 1);
1681 # endif
1682 #endif
1683 #ifdef BFTPDAEMON
1684 /*
1685 * Are we working as the bftp daemon? If so, then ask login
1686 * to start bftp instead of shell.
1687 */
1688 if (bftpd) {
1689 argv = addarg(argv, "-e");
1690 argv = addarg(argv, BFTPPATH);
1691 } else
1692 #endif
1693 #if defined (SECURELOGIN)
1694 /*
1695 * don't worry about the -f that might get sent.
1696 * A -s is supposed to override it anyhow.
1697 */
1698 if (require_secure_login)
1699 argv = addarg(argv, "-s");
1700 #endif
1701 #if defined (AUTHENTICATION)
1702 if (auth_level >= 0 && autologin == AUTH_VALID) {
1703 # if !defined(NO_LOGIN_F)
1704 # if defined(FORWARD)
1705 if (got_forwarded_creds)
1706 argv = addarg(argv, "-F");
1707 else
1708 # endif /* FORWARD */
1709 argv = addarg(argv, "-f");
1710 argv = addarg(argv, "--");
1711 argv = addarg(argv, name);
1712 # else
1713 # if defined(LOGIN_R)
1714 /*
1715 * We don't have support for "login -f", but we
1716 * can fool /bin/login into thinking that we are
1717 * rlogind, and allow us to log in without a
1718 * password. The rlogin protocol expects
1719 * local-user\0remote-user\0term/speed\0
1720 */
1721
1722 if (pty > 2) {
1723 register char *cp;
1724 char speed[128];
1725 int isecho, israw, xpty, len;
1726 extern int def_rspeed;
1727 # ifndef LOGIN_HOST
1728 /*
1729 * Tell login that we are coming from "localhost".
1730 * If we passed in the real host name, then the
1731 * user would have to allow .rhost access from
1732 * every machine that they want authenticated
1733 * access to work from, which sort of defeats
1734 * the purpose of an authenticated login...
1735 * So, we tell login that the session is coming
1736 * from "localhost", and the user will only have
1737 * to have "localhost" in their .rhost file.
1738 */
1739 # define LOGIN_HOST "localhost"
1740 # endif
1741 argv = addarg(argv, "-r");
1742 argv = addarg(argv, LOGIN_HOST);
1743
1744 xpty = pty;
1745 # ifndef STREAMSPTY
1746 pty = 0;
1747 # else
1748 ttyfd = 0;
1749 # endif
1750 init_termbuf();
1751 isecho = tty_isecho();
1752 israw = tty_israw();
1753 if (isecho || !israw) {
1754 tty_setecho(0); /* Turn off echo */
1755 tty_setraw(1); /* Turn on raw */
1756 set_termbuf();
1757 }
1758 len = strlen(name)+1;
1759 write(xpty, name, len);
1760 write(xpty, name, len);
1761 sprintf(speed, "%s/%d", (cp = getenv("TERM")) ? cp : "",
1762 (def_rspeed > 0) ? def_rspeed : 9600);
1763 len = strlen(speed)+1;
1764 write(xpty, speed, len);
1765
1766 if (isecho || !israw) {
1767 init_termbuf();
1768 tty_setecho(isecho);
1769 tty_setraw(israw);
1770 set_termbuf();
1771 if (!israw) {
1772 /*
1773 * Write a newline to ensure
1774 * that login will be able to
1775 * read the line...
1776 */
1777 write(xpty, "\n", 1);
1778 }
1779 }
1780 pty = xpty;
1781 }
1782 # else
1783 argv = addarg(argv, "--");
1784 argv = addarg(argv, name);
1785 # endif
1786 # endif
1787 } else
1788 #endif
1789 if (getenv("USER")) {
1790 argv = addarg(argv, "--");
1791 argv = addarg(argv, getenv("USER"));
1792 #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P)
1793 {
1794 register char **cpp;
1795 for (cpp = environ; *cpp; cpp++)
1796 argv = addarg(argv, *cpp);
1797 }
1798 #endif
1799 /*
1800 * Assume that login will set the USER variable
1801 * correctly. For SysV systems, this means that
1802 * USER will no longer be set, just LOGNAME by
1803 * login. (The problem is that if the auto-login
1804 * fails, and the user then specifies a different
1805 * account name, he can get logged in with both
1806 * LOGNAME and USER in his environment, but the
1807 * USER value will be wrong.
1808 */
1809 unsetenv("USER");
1810 }
1811 #ifdef SOLARIS
1812 else {
1813 char **p;
1814
1815 argv = addarg(argv, ""); /* no login name */
1816 for (p = environ; *p; p++) {
1817 argv = addarg(argv, *p);
1818 }
1819 }
1820 #endif /* SOLARIS */
1821 #if defined(AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R)
1822 if (pty > 2)
1823 close(pty);
1824 #endif
1825 if (getent(defent, gettyname) == 1) {
1826 char *cp = defstrs;
1827
1828 loginprog = getstr("lo", &cp);
1829 }
1830 if (loginprog == NULL)
1831 loginprog = _PATH_LOGIN;
1832 closelog();
1833 /*
1834 * This sleep(1) is in here so that telnetd can
1835 * finish up with the tty. There's a race condition
1836 * the login banner message gets lost...
1837 */
1838 sleep(1);
1839 execv(loginprog, argv);
1840
1841 syslog(LOG_ERR, "%s: %m\n", loginprog);
1842 fatalperror(net, loginprog);
1843 /*NOTREACHED*/
1844 }
1845
1846 char **
1847 addarg(argv, val)
1848 register char **argv;
1849 register char *val;
1850 {
1851 register char **cpp;
1852
1853 if (argv == NULL) {
1854 /*
1855 * 10 entries, a leading length, and a null
1856 */
1857 argv = (char **)malloc(sizeof(*argv) * 12);
1858 if (argv == NULL)
1859 return(NULL);
1860 *argv++ = (char *)10;
1861 *argv = (char *)0;
1862 }
1863 for (cpp = argv; *cpp; cpp++)
1864 ;
1865 if (cpp == &argv[(long)argv[-1]]) {
1866 --argv;
1867 *argv = (char *)((long)(*argv) + 10);
1868 argv = (char **)realloc(argv, sizeof(*argv)*((long)(*argv) + 2));
1869 if (argv == NULL) {
1870 fatal(net, "not enough memory");
1871 /*NOTREACHED*/
1872 }
1873 argv++;
1874 cpp = &argv[(long)argv[-1] - 10];
1875 }
1876 *cpp++ = val;
1877 *cpp = 0;
1878 return(argv);
1879 }
1880 #endif /* NEWINIT */
1881
1882 /*
1883 * scrub_env()
1884 *
1885 * We only accept the environment variables listed below.
1886 */
1887
1888 void
1889 scrub_env()
1890 {
1891 static const char *reject[] = {
1892 "TERMCAP=/",
1893 NULL
1894 };
1895
1896 static const char *accept[] = {
1897 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
1898 "TERM=",
1899 "EDITOR=",
1900 "PAGER=",
1901 "LOGNAME=",
1902 "POSIXLY_CORRECT=",
1903 "TERMCAP=",
1904 "PRINTER=",
1905 NULL
1906 };
1907
1908 char **cpp, **cpp2;
1909 const char **p;
1910
1911 for (cpp2 = cpp = environ; *cpp; cpp++) {
1912 int reject_it = 0;
1913
1914 for(p = reject; *p; p++)
1915 if(strncmp(*cpp, *p, strlen(*p)) == 0) {
1916 reject_it = 1;
1917 break;
1918 }
1919 if (reject_it)
1920 continue;
1921
1922 for(p = accept; *p; p++)
1923 if(strncmp(*cpp, *p, strlen(*p)) == 0)
1924 break;
1925 if(*p != NULL)
1926 *cpp2++ = *cpp;
1927 }
1928 *cpp2 = NULL;
1929 }
1930
1931 /*
1932 * cleanup()
1933 *
1934 * This is the routine to call when we are all through, to
1935 * clean up anything that needs to be cleaned up.
1936 */
1937 /* ARGSUSED */
1938 void
1939 cleanup(sig)
1940 int sig;
1941 {
1942 #ifndef PARENT_DOES_UTMP
1943 # if (BSD > 43) || defined(convex)
1944 char *p, c;
1945
1946 p = line + sizeof("/dev/") - 1;
1947 if (logout(p))
1948 logwtmp(p, "", "");
1949 (void)chmod(line, 0666);
1950 (void)chown(line, 0, 0);
1951 c = *p; *p = 'p';
1952 (void)chmod(line, 0666);
1953 (void)chown(line, 0, 0);
1954 *p = c;
1955 if (ttyaction(line, "telnetd", "root"))
1956 syslog(LOG_ERR, "%s: ttyaction failed", line);
1957 (void) shutdown(net, 2);
1958 exit(1);
1959 # else
1960 void rmut();
1961
1962 rmut();
1963 vhangup(); /* XXX */
1964 (void) shutdown(net, 2);
1965 exit(1);
1966 # endif
1967 #else /* PARENT_DOES_UTMP */
1968 # ifdef NEWINIT
1969 (void) shutdown(net, 2);
1970 exit(1);
1971 # else /* NEWINIT */
1972 # ifdef CRAY
1973 static int incleanup = 0;
1974 register int t;
1975 int child_status; /* status of child process as returned by waitpid */
1976 int flags = WNOHANG|WUNTRACED;
1977
1978 /*
1979 * 1: Pick up the zombie, if we are being called
1980 * as the signal handler.
1981 * 2: If we are a nested cleanup(), return.
1982 * 3: Try to clean up TMPDIR.
1983 * 4: Fill in utmp with shutdown of process.
1984 * 5: Close down the network and pty connections.
1985 * 6: Finish up the TMPDIR cleanup, if needed.
1986 */
1987 if (sig == SIGCHLD) {
1988 while (waitpid(-1, &child_status, flags) > 0)
1989 ; /* VOID */
1990 /* Check if the child process was stopped
1991 * rather than exited. We want cleanup only if
1992 * the child has died.
1993 */
1994 if (WIFSTOPPED(child_status)) {
1995 return;
1996 }
1997 }
1998 t = sigblock(sigmask(SIGCHLD));
1999 if (incleanup) {
2000 sigsetmask(t);
2001 return;
2002 }
2003 incleanup = 1;
2004 sigsetmask(t);
2005 #ifdef UNICOS7x
2006 if (secflag) {
2007 /*
2008 * We need to set ourselves back to a null
2009 * label to clean up.
2010 */
2011
2012 setulvl(sysv.sy_minlvl);
2013 setucmp((long)0);
2014 }
2015 #endif /* UNICOS7x */
2016
2017 t = cleantmp(&wtmp);
2018 setutent(); /* just to make sure */
2019 # endif /* CRAY */
2020 rmut(line);
2021 close(pty);
2022 #ifdef KRB5
2023 kerberos5_cleanup();
2024 #endif
2025 (void) shutdown(net, 2);
2026 # ifdef CRAY
2027 if (t == 0)
2028 cleantmp(&wtmp);
2029 # endif /* CRAY */
2030 exit(1);
2031 # endif /* NEWINT */
2032 #endif /* PARENT_DOES_UTMP */
2033 }
2034
2035 #if defined(PARENT_DOES_UTMP) && !defined(NEWINIT)
2036 /*
2037 * _utmp_sig_rcv
2038 * utmp_sig_init
2039 * utmp_sig_wait
2040 * These three functions are used to coordinate the handling of
2041 * the utmp file between the server and the soon-to-be-login shell.
2042 * The server actually creates the utmp structure, the child calls
2043 * utmp_sig_wait(), until the server calls utmp_sig_notify() and
2044 * signals the future-login shell to proceed.
2045 */
2046 static int caught=0; /* NZ when signal intercepted */
2047 static void (*func)(); /* address of previous handler */
2048
2049 void
2050 _utmp_sig_rcv(sig)
2051 int sig;
2052 {
2053 caught = 1;
2054 (void) signal(SIGUSR1, func);
2055 }
2056
2057 void
2058 utmp_sig_init()
2059 {
2060 /*
2061 * register signal handler for UTMP creation
2062 */
2063 if ((int)(func = signal(SIGUSR1, _utmp_sig_rcv)) == -1)
2064 fatalperror(net, "telnetd/signal");
2065 }
2066
2067 void
2068 utmp_sig_reset()
2069 {
2070 (void) signal(SIGUSR1, func); /* reset handler to default */
2071 }
2072
2073 # ifdef __hpux
2074 # define sigoff() /* do nothing */
2075 # define sigon() /* do nothing */
2076 # endif
2077
2078 void
2079 utmp_sig_wait()
2080 {
2081 /*
2082 * Wait for parent to write our utmp entry.
2083 */
2084 sigoff();
2085 while (caught == 0) {
2086 pause(); /* wait until we get a signal (sigon) */
2087 sigoff(); /* turn off signals while we check caught */
2088 }
2089 sigon(); /* turn on signals again */
2090 }
2091
2092 void
2093 utmp_sig_notify(pid)
2094 {
2095 kill(pid, SIGUSR1);
2096 }
2097
2098 # ifdef CRAY
2099 static int gotsigjob = 0;
2100
2101 /*ARGSUSED*/
2102 void
2103 sigjob(sig)
2104 int sig;
2105 {
2106 register int jid;
2107 register struct jobtemp *jp;
2108
2109 while ((jid = waitjob(NULL)) != -1) {
2110 if (jid == 0) {
2111 return;
2112 }
2113 gotsigjob++;
2114 jobend(jid, NULL, NULL);
2115 }
2116 }
2117
2118 /*
2119 * jid_getutid:
2120 * called by jobend() before calling cleantmp()
2121 * to find the correct $TMPDIR to cleanup.
2122 */
2123
2124 struct utmp *
2125 jid_getutid(jid)
2126 int jid;
2127 {
2128 struct utmp *cur = NULL;
2129
2130 setutent(); /* just to make sure */
2131 while (cur = getutent()) {
2132 if ( (cur->ut_type != NULL) && (jid == cur->ut_jid) ) {
2133 return(cur);
2134 }
2135 }
2136
2137 return(0);
2138 }
2139
2140 /*
2141 * Clean up the TMPDIR that login created.
2142 * The first time this is called we pick up the info
2143 * from the utmp. If the job has already gone away,
2144 * then we'll clean up and be done. If not, then
2145 * when this is called the second time it will wait
2146 * for the signal that the job is done.
2147 */
2148 int
2149 cleantmp(wtp)
2150 register struct utmp *wtp;
2151 {
2152 struct utmp *utp;
2153 static int first = 1;
2154 register int mask, omask, ret;
2155 extern struct utmp *getutid P((const struct utmp *_Id));
2156
2157
2158 mask = sigmask(WJSIGNAL);
2159
2160 if (first == 0) {
2161 omask = sigblock(mask);
2162 while (gotsigjob == 0)
2163 sigpause(omask);
2164 return(1);
2165 }
2166 first = 0;
2167 setutent(); /* just to make sure */
2168
2169 utp = getutid(wtp);
2170 if (utp == 0) {
2171 syslog(LOG_ERR, "Can't get /etc/utmp entry to clean TMPDIR");
2172 return(-1);
2173 }
2174 /*
2175 * Nothing to clean up if the user shell was never started.
2176 */
2177 if (utp->ut_type != USER_PROCESS || utp->ut_jid == 0)
2178 return(1);
2179
2180 /*
2181 * Block the WJSIGNAL while we are in jobend().
2182 */
2183 omask = sigblock(mask);
2184 ret = jobend(utp->ut_jid, utp->ut_tpath, utp->ut_user);
2185 sigsetmask(omask);
2186 return(ret);
2187 }
2188
2189 int
2190 jobend(jid, path, user)
2191 register int jid;
2192 register char *path;
2193 register char *user;
2194 {
2195 static int saved_jid = 0;
2196 static int pty_saved_jid = 0;
2197 static char saved_path[sizeof(wtmp.ut_tpath)+1];
2198 static char saved_user[sizeof(wtmp.ut_user)+1];
2199
2200 /*
2201 * this little piece of code comes into play
2202 * only when ptyreconnect is used to reconnect
2203 * to an previous session.
2204 *
2205 * this is the only time when the
2206 * "saved_jid != jid" code is executed.
2207 */
2208
2209 if ( saved_jid && saved_jid != jid ) {
2210 if (!path) { /* called from signal handler */
2211 pty_saved_jid = jid;
2212 } else {
2213 pty_saved_jid = saved_jid;
2214 }
2215 }
2216
2217 if (path) {
2218 strncpy(saved_path, path, sizeof(wtmp.ut_tpath));
2219 strncpy(saved_user, user, sizeof(wtmp.ut_user));
2220 saved_path[sizeof(saved_path)] = '\0';
2221 saved_user[sizeof(saved_user)] = '\0';
2222 }
2223 if (saved_jid == 0) {
2224 saved_jid = jid;
2225 return(0);
2226 }
2227
2228 /* if the jid has changed, get the correct entry from the utmp file */
2229
2230 if ( saved_jid != jid ) {
2231 struct utmp *utp = NULL;
2232 struct utmp *jid_getutid();
2233
2234 utp = jid_getutid(pty_saved_jid);
2235
2236 if (utp == 0) {
2237 syslog(LOG_ERR, "Can't get /etc/utmp entry to clean TMPDIR");
2238 return(-1);
2239 }
2240
2241 cleantmpdir(jid, utp->ut_tpath, utp->ut_user);
2242 return(1);
2243 }
2244
2245 cleantmpdir(jid, saved_path, saved_user);
2246 return(1);
2247 }
2248
2249 /*
2250 * Fork a child process to clean up the TMPDIR
2251 */
2252 cleantmpdir(jid, tpath, user)
2253 register int jid;
2254 register char *tpath;
2255 register char *user;
2256 {
2257 switch(fork()) {
2258 case -1:
2259 syslog(LOG_ERR, "TMPDIR cleanup(%s): fork() failed: %m\n",
2260 tpath);
2261 break;
2262 case 0:
2263 execl(CLEANTMPCMD, CLEANTMPCMD, user, tpath, 0);
2264 syslog(LOG_ERR, "TMPDIR cleanup(%s): execl(%s) failed: %m\n",
2265 tpath, CLEANTMPCMD);
2266 exit(1);
2267 default:
2268 /*
2269 * Forget about child. We will exit, and
2270 * /etc/init will pick it up.
2271 */
2272 break;
2273 }
2274 }
2275 # endif /* CRAY */
2276 #endif /* defined(PARENT_DOES_UTMP) && !defined(NEWINIT) */
2277
2278 /*
2279 * rmut()
2280 *
2281 * This is the function called by cleanup() to
2282 * remove the utmp entry for this person.
2283 */
2284
2285 #ifdef UTMPX
2286 void
2287 rmut()
2288 {
2289 register f;
2290 int found = 0;
2291 struct utmp *u, *utmp;
2292 int nutmp;
2293 struct stat statbf;
2294
2295 struct utmpx *utxp, utmpx;
2296
2297 /*
2298 * This updates the utmpx and utmp entries and make a wtmp/x entry
2299 */
2300
2301 SCPYN(utmpx.ut_line, line + sizeof("/dev/") - 1);
2302 utxp = getutxline(&utmpx);
2303 if (utxp) {
2304 utxp->ut_type = DEAD_PROCESS;
2305 utxp->ut_exit.e_termination = 0;
2306 utxp->ut_exit.e_exit = 0;
2307 (void) time(&utmpx.ut_tv.tv_sec);
2308 utmpx.ut_tv.tv_usec = 0;
2309 modutx(utxp);
2310 }
2311 endutxent();
2312 } /* end of rmut */
2313 #endif
2314
2315 #if !defined(UTMPX) && !(defined(CRAY) || defined(__hpux)) && BSD <= 43
2316 void
2317 rmut()
2318 {
2319 register f;
2320 int found = 0;
2321 struct utmp *u, *utmp;
2322 int nutmp;
2323 struct stat statbf;
2324
2325 f = open(utmpf, O_RDWR);
2326 if (f >= 0) {
2327 (void) fstat(f, &statbf);
2328 utmp = (struct utmp *)malloc((unsigned)statbf.st_size);
2329 if (!utmp)
2330 syslog(LOG_ERR, "utmp malloc failed");
2331 if (statbf.st_size && utmp) {
2332 nutmp = read(f, (char *)utmp, (int)statbf.st_size);
2333 nutmp /= sizeof(struct utmp);
2334
2335 for (u = utmp ; u < &utmp[nutmp] ; u++) {
2336 if (SCMPN(u->ut_line, line+5) ||
2337 u->ut_name[0]==0)
2338 continue;
2339 (void)lseek(f, (off_t)((long)u)-((long)utmp),
2340 SEEK_SET);
2341 SCPYN(u->ut_name, "");
2342 SCPYN(u->ut_host, "");
2343 (void) time(&u->ut_time);
2344 (void) write(f, (char *)u, sizeof(wtmp));
2345 found++;
2346 }
2347 }
2348 (void) close(f);
2349 }
2350 if (found) {
2351 f = open(wtmpf, O_WRONLY|O_APPEND);
2352 if (f >= 0) {
2353 SCPYN(wtmp.ut_line, line+5);
2354 SCPYN(wtmp.ut_name, "");
2355 SCPYN(wtmp.ut_host, "");
2356 (void) time(&wtmp.ut_time);
2357 (void) write(f, (char *)&wtmp, sizeof(wtmp));
2358 (void) close(f);
2359 }
2360 }
2361 (void) chmod(line, 0666);
2362 (void) chown(line, 0, 0);
2363 line[strlen("/dev/")] = 'p';
2364 (void) chmod(line, 0666);
2365 (void) chown(line, 0, 0);
2366 } /* end of rmut */
2367 #endif /* CRAY */
2368
2369 #ifdef __hpux
2370 rmut (line)
2371 char *line;
2372 {
2373 struct utmp utmp;
2374 struct utmp *utptr;
2375 int fd; /* for /etc/wtmp */
2376
2377 utmp.ut_type = USER_PROCESS;
2378 (void) strncpy(utmp.ut_id, line+12, sizeof(utmp.ut_id));
2379 (void) setutent();
2380 utptr = getutid(&utmp);
2381 /* write it out only if it exists */
2382 if (utptr) {
2383 utptr->ut_type = DEAD_PROCESS;
2384 utptr->ut_time = time((long *) 0);
2385 (void) pututline(utptr);
2386 /* set wtmp entry if wtmp file exists */
2387 if ((fd = open(wtmpf, O_WRONLY | O_APPEND)) >= 0) {
2388 (void) write(fd, utptr, sizeof(utmp));
2389 (void) close(fd);
2390 }
2391 }
2392 (void) endutent();
2393
2394 (void) chmod(line, 0666);
2395 (void) chown(line, 0, 0);
2396 line[14] = line[13];
2397 line[13] = line[12];
2398 line[8] = 'm';
2399 line[9] = '/';
2400 line[10] = 'p';
2401 line[11] = 't';
2402 line[12] = 'y';
2403 (void) chmod(line, 0666);
2404 (void) chown(line, 0, 0);
2405 }
2406 #endif
2407