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