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#if 0 35#ifndef lint 36static const char sccsid[] = "@(#)sys_term.c 8.4+1 (Berkeley) 5/30/95"; 37#endif 38#endif 39#include <sys/cdefs.h> 40__FBSDID("$FreeBSD: src/contrib/telnet/telnetd/sys_term.c,v 1.18 2003/05/04 02:54:49 obrien Exp $"); 41 42#include <sys/types.h> 43#include <libutil.h> 44#include <stdlib.h> 45#if (!defined(__BEOS__) && !defined(__HAIKU__)) 46# include <sys/tty.h> 47# include <utmp.h> 48#endif 49 50#include "telnetd.h" 51#include "pathnames.h" 52 53#ifdef AUTHENTICATION 54#include <libtelnet/auth.h> 55#endif 56 57int cleanopen(char *); 58void scrub_env(void); 59 60#if (!defined(__BEOS__) && !defined(__HAIKU__)) 61 62struct utmp wtmp; 63 64# ifdef _PATH_WTMP 65char wtmpf[] = _PATH_WTMP; 66# else 67char wtmpf[] = "/var/log/wtmp"; 68# endif 69# ifdef _PATH_UTMP 70char utmpf[] = _PATH_UTMP; 71# else 72char utmpf[] = "/var/run/utmp"; 73# endif 74#endif 75 76char *envinit[3]; 77extern char **environ; 78 79#define SCPYN(a, b) (void) strncpy(a, b, sizeof(a)) 80#define SCMPN(a, b) strncmp(a, b, sizeof(a)) 81 82#ifdef t_erase 83#undef t_erase 84#undef t_kill 85#undef t_intrc 86#undef t_quitc 87#undef t_startc 88#undef t_stopc 89#undef t_eofc 90#undef t_brkc 91#undef t_suspc 92#undef t_dsuspc 93#undef t_rprntc 94#undef t_flushc 95#undef t_werasc 96#undef t_lnextc 97#endif 98 99#ifndef USE_TERMIO 100struct termbuf { 101 struct sgttyb sg; 102 struct tchars tc; 103 struct ltchars ltc; 104 int state; 105 int lflags; 106} termbuf, termbuf2; 107# define cfsetospeed(tp, val) (tp)->sg.sg_ospeed = (val) 108# define cfsetispeed(tp, val) (tp)->sg.sg_ispeed = (val) 109# define cfgetospeed(tp) (tp)->sg.sg_ospeed 110# define cfgetispeed(tp) (tp)->sg.sg_ispeed 111#else /* USE_TERMIO */ 112# ifndef TCSANOW 113# ifdef TCSETS 114# define TCSANOW TCSETS 115# define TCSADRAIN TCSETSW 116# define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t) 117# else 118# ifdef TCSETA 119# define TCSANOW TCSETA 120# define TCSADRAIN TCSETAW 121# define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t) 122# else 123# define TCSANOW TIOCSETA 124# define TCSADRAIN TIOCSETAW 125# define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t) 126# endif 127# endif 128# define tcsetattr(f, a, t) ioctl(f, a, t) 129# define cfsetospeed(tp, val) (tp)->c_cflag &= ~CBAUD; \ 130 (tp)->c_cflag |= (val) 131# define cfgetospeed(tp) ((tp)->c_cflag & CBAUD) 132# ifdef CIBAUD 133# define cfsetispeed(tp, val) (tp)->c_cflag &= ~CIBAUD; \ 134 (tp)->c_cflag |= ((val)<<IBSHIFT) 135# define cfgetispeed(tp) (((tp)->c_cflag & CIBAUD)>>IBSHIFT) 136# else 137# define cfsetispeed(tp, val) (tp)->c_cflag &= ~CBAUD; \ 138 (tp)->c_cflag |= (val) 139# define cfgetispeed(tp) ((tp)->c_cflag & CBAUD) 140# endif 141# endif /* TCSANOW */ 142struct termios termbuf, termbuf2; /* pty control structure */ 143#endif /* USE_TERMIO */ 144 145#include <sys/types.h> 146#include <libutil.h> 147 148int cleanopen(char *); 149void scrub_env(void); 150static char **addarg(char **, const char *); 151 152/* 153 * init_termbuf() 154 * copy_termbuf(cp) 155 * set_termbuf() 156 * 157 * These three routines are used to get and set the "termbuf" structure 158 * to and from the kernel. init_termbuf() gets the current settings. 159 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and 160 * set_termbuf() writes the structure into the kernel. 161 */ 162 163void 164init_termbuf(void) 165{ 166#ifndef USE_TERMIO 167 (void) ioctl(pty, TIOCGETP, (char *)&termbuf.sg); 168 (void) ioctl(pty, TIOCGETC, (char *)&termbuf.tc); 169 (void) ioctl(pty, TIOCGLTC, (char *)&termbuf.ltc); 170# ifdef TIOCGSTATE 171 (void) ioctl(pty, TIOCGSTATE, (char *)&termbuf.state); 172# endif 173#else 174 (void) tcgetattr(pty, &termbuf); 175#endif 176 termbuf2 = termbuf; 177} 178 179#if defined(LINEMODE) && defined(TIOCPKT_IOCTL) 180void 181copy_termbuf(char *cp, size_t len) 182{ 183 if (len > sizeof(termbuf)) 184 len = sizeof(termbuf); 185 memmove((char *)&termbuf, cp, len); 186 termbuf2 = termbuf; 187} 188#endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */ 189 190void 191set_termbuf(void) 192{ 193 /* 194 * Only make the necessary changes. 195 */ 196#ifndef USE_TERMIO 197 if (memcmp((char *)&termbuf.sg, (char *)&termbuf2.sg, 198 sizeof(termbuf.sg))) 199 (void) ioctl(pty, TIOCSETN, (char *)&termbuf.sg); 200 if (memcmp((char *)&termbuf.tc, (char *)&termbuf2.tc, 201 sizeof(termbuf.tc))) 202 (void) ioctl(pty, TIOCSETC, (char *)&termbuf.tc); 203 if (memcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc, 204 sizeof(termbuf.ltc))) 205 (void) ioctl(pty, TIOCSLTC, (char *)&termbuf.ltc); 206 if (termbuf.lflags != termbuf2.lflags) 207 (void) ioctl(pty, TIOCLSET, (char *)&termbuf.lflags); 208#else /* USE_TERMIO */ 209 if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf))) 210 (void) tcsetattr(pty, TCSANOW, &termbuf); 211#endif /* USE_TERMIO */ 212} 213 214 215/* 216 * spcset(func, valp, valpp) 217 * 218 * This function takes various special characters (func), and 219 * sets *valp to the current value of that character, and 220 * *valpp to point to where in the "termbuf" structure that 221 * value is kept. 222 * 223 * It returns the SLC_ level of support for this function. 224 */ 225 226#ifndef USE_TERMIO 227int 228spcset(int func, cc_t *valp, cc_t **valpp) 229{ 230 switch(func) { 231 case SLC_EOF: 232 *valp = termbuf.tc.t_eofc; 233 *valpp = (cc_t *)&termbuf.tc.t_eofc; 234 return(SLC_VARIABLE); 235 case SLC_EC: 236 *valp = termbuf.sg.sg_erase; 237 *valpp = (cc_t *)&termbuf.sg.sg_erase; 238 return(SLC_VARIABLE); 239 case SLC_EL: 240 *valp = termbuf.sg.sg_kill; 241 *valpp = (cc_t *)&termbuf.sg.sg_kill; 242 return(SLC_VARIABLE); 243 case SLC_IP: 244 *valp = termbuf.tc.t_intrc; 245 *valpp = (cc_t *)&termbuf.tc.t_intrc; 246 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT); 247 case SLC_ABORT: 248 *valp = termbuf.tc.t_quitc; 249 *valpp = (cc_t *)&termbuf.tc.t_quitc; 250 return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT); 251 case SLC_XON: 252 *valp = termbuf.tc.t_startc; 253 *valpp = (cc_t *)&termbuf.tc.t_startc; 254 return(SLC_VARIABLE); 255 case SLC_XOFF: 256 *valp = termbuf.tc.t_stopc; 257 *valpp = (cc_t *)&termbuf.tc.t_stopc; 258 return(SLC_VARIABLE); 259 case SLC_AO: 260 *valp = termbuf.ltc.t_flushc; 261 *valpp = (cc_t *)&termbuf.ltc.t_flushc; 262 return(SLC_VARIABLE); 263 case SLC_SUSP: 264 *valp = termbuf.ltc.t_suspc; 265 *valpp = (cc_t *)&termbuf.ltc.t_suspc; 266 return(SLC_VARIABLE); 267 case SLC_EW: 268 *valp = termbuf.ltc.t_werasc; 269 *valpp = (cc_t *)&termbuf.ltc.t_werasc; 270 return(SLC_VARIABLE); 271 case SLC_RP: 272 *valp = termbuf.ltc.t_rprntc; 273 *valpp = (cc_t *)&termbuf.ltc.t_rprntc; 274 return(SLC_VARIABLE); 275 case SLC_LNEXT: 276 *valp = termbuf.ltc.t_lnextc; 277 *valpp = (cc_t *)&termbuf.ltc.t_lnextc; 278 return(SLC_VARIABLE); 279 case SLC_FORW1: 280 *valp = termbuf.tc.t_brkc; 281 *valpp = (cc_t *)&termbuf.ltc.t_lnextc; 282 return(SLC_VARIABLE); 283 case SLC_BRK: 284 case SLC_SYNCH: 285 case SLC_AYT: 286 case SLC_EOR: 287 *valp = (cc_t)0; 288 *valpp = (cc_t *)0; 289 return(SLC_DEFAULT); 290 default: 291 *valp = (cc_t)0; 292 *valpp = (cc_t *)0; 293 return(SLC_NOSUPPORT); 294 } 295} 296 297#else /* USE_TERMIO */ 298 299 300#define setval(a, b) *valp = termbuf.c_cc[a]; \ 301 *valpp = &termbuf.c_cc[a]; \ 302 return(b); 303#define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT); 304 305int 306spcset(int func, cc_t *valp, cc_t **valpp) 307{ 308 switch(func) { 309 case SLC_EOF: 310 setval(VEOF, SLC_VARIABLE); 311 case SLC_EC: 312 setval(VERASE, SLC_VARIABLE); 313 case SLC_EL: 314 setval(VKILL, SLC_VARIABLE); 315 case SLC_IP: 316 setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT); 317 case SLC_ABORT: 318 setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT); 319 case SLC_XON: 320#ifdef VSTART 321 setval(VSTART, SLC_VARIABLE); 322#else 323 defval(0x13); 324#endif 325 case SLC_XOFF: 326#ifdef VSTOP 327 setval(VSTOP, SLC_VARIABLE); 328#else 329 defval(0x11); 330#endif 331 case SLC_EW: 332#ifdef VWERASE 333 setval(VWERASE, SLC_VARIABLE); 334#else 335 defval(0); 336#endif 337 case SLC_RP: 338#ifdef VREPRINT 339 setval(VREPRINT, SLC_VARIABLE); 340#else 341 defval(0); 342#endif 343 case SLC_LNEXT: 344#ifdef VLNEXT 345 setval(VLNEXT, SLC_VARIABLE); 346#else 347 defval(0); 348#endif 349 case SLC_AO: 350#if !defined(VDISCARD) && defined(VFLUSHO) 351# define VDISCARD VFLUSHO 352#endif 353#ifdef VDISCARD 354 setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT); 355#else 356 defval(0); 357#endif 358 case SLC_SUSP: 359#ifdef VSUSP 360 setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN); 361#else 362 defval(0); 363#endif 364#ifdef VEOL 365 case SLC_FORW1: 366 setval(VEOL, SLC_VARIABLE); 367#endif 368#ifdef VEOL2 369 case SLC_FORW2: 370 setval(VEOL2, SLC_VARIABLE); 371#endif 372 case SLC_AYT: 373#ifdef VSTATUS 374 setval(VSTATUS, SLC_VARIABLE); 375#else 376 defval(0); 377#endif 378 379 case SLC_BRK: 380 case SLC_SYNCH: 381 case SLC_EOR: 382 defval(0); 383 384 default: 385 *valp = 0; 386 *valpp = 0; 387 return(SLC_NOSUPPORT); 388 } 389} 390#endif /* USE_TERMIO */ 391 392/* 393 * getpty() 394 * 395 * Allocate a pty. As a side effect, the external character 396 * array "line" contains the name of the slave side. 397 * 398 * Returns the file descriptor of the opened pty. 399 */ 400char alpha[] = "0123456789abcdefghijklmnopqrstuv"; 401char line[16]; 402 403int 404getpty(int *ptynum __unused) 405{ 406 int p; 407 const char *cp; 408 char *p1, *p2; 409 int i; 410 411#if (defined(__BEOS__) || defined(__HAIKU__)) 412 (void) strcpy(line, "/dev/pt/XX"); 413#else 414 (void) strcpy(line, _PATH_DEV); 415 (void) strcat(line, "ptyXX"); 416#endif 417 p1 = &line[8]; 418 p2 = &line[9]; 419 420 for (cp = "pqrsPQRS"; *cp; cp++) { 421 struct stat stb; 422 423 *p1 = *cp; 424 *p2 = '0'; 425 /* 426 * This stat() check is just to keep us from 427 * looping through all 256 combinations if there 428 * aren't that many ptys available. 429 */ 430 if (stat(line, &stb) < 0) 431 break; 432 for (i = 0; i < 32; i++) { 433 *p2 = alpha[i]; 434 p = open(line, 2); 435 if (p > 0) { 436 line[5] = 't'; 437 chown(line, 0, 0); 438 chmod(line, 0600); 439 return(p); 440 } 441 } 442 } 443 return(-1); 444} 445 446#ifdef LINEMODE 447/* 448 * tty_flowmode() Find out if flow control is enabled or disabled. 449 * tty_linemode() Find out if linemode (external processing) is enabled. 450 * tty_setlinemod(on) Turn on/off linemode. 451 * tty_isecho() Find out if echoing is turned on. 452 * tty_setecho(on) Enable/disable character echoing. 453 * tty_israw() Find out if terminal is in RAW mode. 454 * tty_binaryin(on) Turn on/off BINARY on input. 455 * tty_binaryout(on) Turn on/off BINARY on output. 456 * tty_isediting() Find out if line editing is enabled. 457 * tty_istrapsig() Find out if signal trapping is enabled. 458 * tty_setedit(on) Turn on/off line editing. 459 * tty_setsig(on) Turn on/off signal trapping. 460 * tty_issofttab() Find out if tab expansion is enabled. 461 * tty_setsofttab(on) Turn on/off soft tab expansion. 462 * tty_islitecho() Find out if typed control chars are echoed literally 463 * tty_setlitecho() Turn on/off literal echo of control chars 464 * tty_tspeed(val) Set transmit speed to val. 465 * tty_rspeed(val) Set receive speed to val. 466 */ 467 468 469int 470tty_linemode(void) 471{ 472#ifndef USE_TERMIO 473 return(termbuf.state & TS_EXTPROC); 474#else 475 return(termbuf.c_lflag & EXTPROC); 476#endif 477} 478 479void 480tty_setlinemode(int on) 481{ 482#ifdef TIOCEXT 483 set_termbuf(); 484 (void) ioctl(pty, TIOCEXT, (char *)&on); 485 init_termbuf(); 486#else /* !TIOCEXT */ 487# ifdef EXTPROC 488 if (on) 489 termbuf.c_lflag |= EXTPROC; 490 else 491 termbuf.c_lflag &= ~EXTPROC; 492# endif 493#endif /* TIOCEXT */ 494} 495#endif /* LINEMODE */ 496 497int 498tty_isecho(void) 499{ 500#ifndef USE_TERMIO 501 return (termbuf.sg.sg_flags & ECHO); 502#else 503 return (termbuf.c_lflag & ECHO); 504#endif 505} 506 507int 508tty_flowmode(void) 509{ 510#ifndef USE_TERMIO 511 return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0); 512#else 513 return((termbuf.c_iflag & IXON) ? 1 : 0); 514#endif 515} 516 517int 518tty_restartany(void) 519{ 520#ifndef USE_TERMIO 521# ifdef DECCTQ 522 return((termbuf.lflags & DECCTQ) ? 0 : 1); 523# else 524 return(-1); 525# endif 526#else 527 return((termbuf.c_iflag & IXANY) ? 1 : 0); 528#endif 529} 530 531void 532tty_setecho(int on) 533{ 534#ifndef USE_TERMIO 535 if (on) 536 termbuf.sg.sg_flags |= ECHO|CRMOD; 537 else 538 termbuf.sg.sg_flags &= ~(ECHO|CRMOD); 539#else 540 if (on) 541 termbuf.c_lflag |= ECHO; 542 else 543 termbuf.c_lflag &= ~ECHO; 544#endif 545} 546 547int 548tty_israw(void) 549{ 550#ifndef USE_TERMIO 551 return(termbuf.sg.sg_flags & RAW); 552#else 553 return(!(termbuf.c_lflag & ICANON)); 554#endif 555} 556 557#ifdef AUTHENTICATION 558#if defined(NO_LOGIN_F) && defined(LOGIN_R) 559int 560tty_setraw(int on) 561{ 562# ifndef USE_TERMIO 563 if (on) 564 termbuf.sg.sg_flags |= RAW; 565 else 566 termbuf.sg.sg_flags &= ~RAW; 567# else 568 if (on) 569 termbuf.c_lflag &= ~ICANON; 570 else 571 termbuf.c_lflag |= ICANON; 572# endif 573} 574#endif 575#endif /* AUTHENTICATION */ 576 577void 578tty_binaryin(int on) 579{ 580#ifndef USE_TERMIO 581 if (on) 582 termbuf.lflags |= LPASS8; 583 else 584 termbuf.lflags &= ~LPASS8; 585#else 586 if (on) { 587 termbuf.c_iflag &= ~ISTRIP; 588 } else { 589 termbuf.c_iflag |= ISTRIP; 590 } 591#endif 592} 593 594void 595tty_binaryout(int on) 596{ 597#ifndef USE_TERMIO 598 if (on) 599 termbuf.lflags |= LLITOUT; 600 else 601 termbuf.lflags &= ~LLITOUT; 602#else 603 if (on) { 604 termbuf.c_cflag &= ~(CSIZE|PARENB); 605 termbuf.c_cflag |= CS8; 606 termbuf.c_oflag &= ~OPOST; 607 } else { 608 termbuf.c_cflag &= ~CSIZE; 609 termbuf.c_cflag |= CS7|PARENB; 610 termbuf.c_oflag |= OPOST; 611 } 612#endif 613} 614 615int 616tty_isbinaryin(void) 617{ 618#ifndef USE_TERMIO 619 return(termbuf.lflags & LPASS8); 620#else 621 return(!(termbuf.c_iflag & ISTRIP)); 622#endif 623} 624 625int 626tty_isbinaryout(void) 627{ 628#ifndef USE_TERMIO 629 return(termbuf.lflags & LLITOUT); 630#else 631 return(!(termbuf.c_oflag&OPOST)); 632#endif 633} 634 635#ifdef LINEMODE 636int 637tty_isediting(void) 638{ 639#ifndef USE_TERMIO 640 return(!(termbuf.sg.sg_flags & (CBREAK|RAW))); 641#else 642 return(termbuf.c_lflag & ICANON); 643#endif 644} 645 646int 647tty_istrapsig(void) 648{ 649#ifndef USE_TERMIO 650 return(!(termbuf.sg.sg_flags&RAW)); 651#else 652 return(termbuf.c_lflag & ISIG); 653#endif 654} 655 656void 657tty_setedit(int on) 658{ 659#ifndef USE_TERMIO 660 if (on) 661 termbuf.sg.sg_flags &= ~CBREAK; 662 else 663 termbuf.sg.sg_flags |= CBREAK; 664#else 665 if (on) 666 termbuf.c_lflag |= ICANON; 667 else 668 termbuf.c_lflag &= ~ICANON; 669#endif 670} 671 672void 673tty_setsig(int on) 674{ 675#ifndef USE_TERMIO 676 if (on) 677 ; 678#else 679 if (on) 680 termbuf.c_lflag |= ISIG; 681 else 682 termbuf.c_lflag &= ~ISIG; 683#endif 684} 685#endif /* LINEMODE */ 686 687int 688tty_issofttab(void) 689{ 690#ifndef USE_TERMIO 691 return (termbuf.sg.sg_flags & XTABS); 692#else 693# ifdef OXTABS 694 return (termbuf.c_oflag & OXTABS); 695# endif 696# ifdef TABDLY 697 return ((termbuf.c_oflag & TABDLY) == TAB3); 698# endif 699#endif 700} 701 702void 703tty_setsofttab(int on) 704{ 705#ifndef USE_TERMIO 706 if (on) 707 termbuf.sg.sg_flags |= XTABS; 708 else 709 termbuf.sg.sg_flags &= ~XTABS; 710#else 711 if (on) { 712# ifdef OXTABS 713 termbuf.c_oflag |= OXTABS; 714# endif 715# ifdef TABDLY 716 termbuf.c_oflag &= ~TABDLY; 717 termbuf.c_oflag |= TAB3; 718# endif 719 } else { 720# ifdef OXTABS 721 termbuf.c_oflag &= ~OXTABS; 722# endif 723# ifdef TABDLY 724 termbuf.c_oflag &= ~TABDLY; 725 termbuf.c_oflag |= TAB0; 726# endif 727 } 728#endif 729} 730 731int 732tty_islitecho(void) 733{ 734#ifndef USE_TERMIO 735 return (!(termbuf.lflags & LCTLECH)); 736#else 737# ifdef ECHOCTL 738 return (!(termbuf.c_lflag & ECHOCTL)); 739# endif 740# ifdef TCTLECH 741 return (!(termbuf.c_lflag & TCTLECH)); 742# endif 743# if !defined(ECHOCTL) && !defined(TCTLECH) 744 return (0); /* assumes ctl chars are echoed '^x' */ 745# endif 746#endif 747} 748 749void 750tty_setlitecho(int on) 751{ 752#ifndef USE_TERMIO 753 if (on) 754 termbuf.lflags &= ~LCTLECH; 755 else 756 termbuf.lflags |= LCTLECH; 757#else 758# ifdef ECHOCTL 759 if (on) 760 termbuf.c_lflag &= ~ECHOCTL; 761 else 762 termbuf.c_lflag |= ECHOCTL; 763# endif 764# ifdef TCTLECH 765 if (on) 766 termbuf.c_lflag &= ~TCTLECH; 767 else 768 termbuf.c_lflag |= TCTLECH; 769# endif 770#endif 771} 772 773int 774tty_iscrnl(void) 775{ 776#ifndef USE_TERMIO 777 return (termbuf.sg.sg_flags & CRMOD); 778#else 779 return (termbuf.c_iflag & ICRNL); 780#endif 781} 782 783/* 784 * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD). 785 */ 786#if B4800 != 4800 787#define DECODE_BAUD 788#endif 789 790#ifdef DECODE_BAUD 791 792/* 793 * A table of available terminal speeds 794 */ 795struct termspeeds { 796 int speed; 797 int value; 798} termspeeds[] = { 799 { 0, B0 }, { 50, B50 }, { 75, B75 }, 800 { 110, B110 }, { 134, B134 }, { 150, B150 }, 801 { 200, B200 }, { 300, B300 }, { 600, B600 }, 802 { 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 }, 803 { 4800, B4800 }, 804#ifdef B7200 805 { 7200, B7200 }, 806#endif 807 { 9600, B9600 }, 808#ifdef B14400 809 { 14400, B14400 }, 810#endif 811#ifdef B19200 812 { 19200, B19200 }, 813#endif 814#ifdef B28800 815 { 28800, B28800 }, 816#endif 817#ifdef B38400 818 { 38400, B38400 }, 819#endif 820#ifdef B57600 821 { 57600, B57600 }, 822#endif 823#ifdef B115200 824 { 115200, B115200 }, 825#endif 826#ifdef B230400 827 { 230400, B230400 }, 828#endif 829 { -1, 0 } 830}; 831#endif /* DECODE_BAUD */ 832 833void 834tty_tspeed(int val) 835{ 836#ifdef DECODE_BAUD 837 struct termspeeds *tp; 838 839 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++) 840 ; 841 if (tp->speed == -1) /* back up to last valid value */ 842 --tp; 843 cfsetospeed(&termbuf, tp->value); 844#else /* DECODE_BAUD */ 845 cfsetospeed(&termbuf, val); 846#endif /* DECODE_BAUD */ 847} 848 849void 850tty_rspeed(int val) 851{ 852#ifdef DECODE_BAUD 853 struct termspeeds *tp; 854 855 for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++) 856 ; 857 if (tp->speed == -1) /* back up to last valid value */ 858 --tp; 859 cfsetispeed(&termbuf, tp->value); 860#else /* DECODE_BAUD */ 861 cfsetispeed(&termbuf, val); 862#endif /* DECODE_BAUD */ 863} 864 865/* 866 * getptyslave() 867 * 868 * Open the slave side of the pty, and do any initialization 869 * that is necessary. 870 */ 871static void 872getptyslave(void) 873{ 874 int t = -1; 875 char erase; 876 877# ifdef LINEMODE 878 int waslm; 879# endif 880# ifdef TIOCGWINSZ 881 struct winsize ws; 882 extern int def_row, def_col; 883# endif 884 extern int def_tspeed, def_rspeed; 885 /* 886 * Opening the slave side may cause initilization of the 887 * kernel tty structure. We need remember the state of 888 * if linemode was turned on 889 * terminal window size 890 * terminal speed 891 * erase character 892 * so that we can re-set them if we need to. 893 */ 894# ifdef LINEMODE 895 waslm = tty_linemode(); 896# endif 897 erase = termbuf.c_cc[VERASE]; 898 899 /* 900 * Make sure that we don't have a controlling tty, and 901 * that we are the session (process group) leader. 902 */ 903# ifdef TIOCNOTTY 904 t = open(_PATH_TTY, O_RDWR); 905 if (t >= 0) { 906 (void) ioctl(t, TIOCNOTTY, (char *)0); 907 (void) close(t); 908 } 909# endif 910 911 t = cleanopen(line); 912 if (t < 0) 913 fatalperror(net, line); 914 915 916 /* 917 * set up the tty modes as we like them to be. 918 */ 919 init_termbuf(); 920# ifdef TIOCGWINSZ 921 if (def_row || def_col) { 922 memset((char *)&ws, 0, sizeof(ws)); 923 ws.ws_col = def_col; 924 ws.ws_row = def_row; 925 (void)ioctl(t, TIOCSWINSZ, (char *)&ws); 926 } 927# endif 928 929 /* 930 * Settings for sgtty based systems 931 */ 932# ifndef USE_TERMIO 933 termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS; 934# endif /* USE_TERMIO */ 935 936 /* 937 * Settings for all other termios/termio based 938 * systems, other than 4.4BSD. In 4.4BSD the 939 * kernel does the initial terminal setup. 940 */ 941 tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600); 942 tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600); 943 if (erase) 944 termbuf.c_cc[VERASE] = erase; 945# ifdef LINEMODE 946 if (waslm) 947 tty_setlinemode(1); 948# endif /* LINEMODE */ 949 950 /* 951 * Set the tty modes, and make this our controlling tty. 952 */ 953 set_termbuf(); 954 if (login_tty(t) == -1) 955 fatalperror(net, "login_tty"); 956 if (net > 2) 957 (void) close(net); 958#ifdef AUTHENTICATION 959#if defined(NO_LOGIN_F) && defined(LOGIN_R) 960 /* 961 * Leave the pty open so that we can write out the rlogin 962 * protocol for /bin/login, if the authentication works. 963 */ 964#else 965 if (pty > 2) { 966 (void) close(pty); 967 pty = -1; 968 } 969#endif 970#endif /* AUTHENTICATION */ 971} 972 973#ifndef O_NOCTTY 974#define O_NOCTTY 0 975#endif 976/* 977 * Open the specified slave side of the pty, 978 * making sure that we have a clean tty. 979 */ 980int 981cleanopen(char *li) 982{ 983 int t; 984 985 /* 986 * Make sure that other people can't open the 987 * slave side of the connection. 988 */ 989 (void) chown(li, 0, 0); 990 (void) chmod(li, 0600); 991 992#if (!defined(__BEOS__) && !defined(__HAIKU__)) 993 (void) revoke(li); 994#endif 995 996 t = open(line, O_RDWR|O_NOCTTY); 997 998 if (t < 0) 999 return(-1); 1000 1001 return(t); 1002} 1003 1004 1005#if (defined(__BEOS__) || defined(__HAIKU__)) 1006/* taken from DragonFly's telnetd */ 1007int 1008login_tty(int t) 1009{ 1010 if (setsid() < 0) { 1011 fatalperror(net, "setsid()"); 1012 } 1013 1014 /* 1015 * We get our controlling tty assigned as a side-effect 1016 * of opening up a tty device. But on BSD based systems, 1017 * this only happens if our process group is zero. The 1018 * setsid() call above may have set our pgrp, so clear 1019 * it out before opening the tty... 1020 */ 1021 setpgid(0, 0); 1022 close(open(line, O_RDWR)); 1023 1024 setenv("TTY", line, 1); 1025 1026 if (t != 0) 1027 (void) dup2(t, 0); 1028 if (t != 1) 1029 (void) dup2(t, 1); 1030 if (t != 2) 1031 (void) dup2(t, 2); 1032 if (t > 2) 1033 close(t); 1034 return(0); 1035} 1036#endif /* __BEOS__ */ 1037 1038/* 1039 * startslave(host) 1040 * 1041 * Given a hostname, do whatever 1042 * is necessary to startup the login process on the slave side of the pty. 1043 */ 1044 1045/* ARGSUSED */ 1046void 1047startslave(char *host, int autologin, char *autoname) 1048{ 1049 int i; 1050 1051#ifdef AUTHENTICATION 1052 if (!autoname || !autoname[0]) 1053 autologin = 0; 1054 1055 if (autologin < auth_level) { 1056 fatal(net, "Authorization failed"); 1057 exit(1); 1058 } 1059#endif 1060 1061 1062 if ((i = fork()) < 0) 1063 fatalperror(net, "fork"); 1064 if (i) { 1065 } else { 1066 getptyslave(); 1067 start_login(host, autologin, autoname); 1068 /*NOTREACHED*/ 1069 } 1070} 1071 1072void 1073init_env(void) 1074{ 1075 char **envp; 1076 1077 envp = envinit; 1078 if ((*envp = getenv("TZ"))) 1079 *envp++ -= 3; 1080 *envp = 0; 1081 environ = envinit; 1082} 1083 1084 1085/* 1086 * start_login(host) 1087 * 1088 * Assuming that we are now running as a child processes, this 1089 * function will turn us into the login process. 1090 */ 1091 1092#ifndef AUTHENTICATION 1093#define undef1 __unused 1094#else 1095#define undef1 1096#endif 1097 1098void 1099start_login(char *host undef1, int autologin undef1, char *name undef1) 1100{ 1101 char **argv; 1102 1103 scrub_env(); 1104 1105 /* 1106 * -h : pass on name of host. 1107 * WARNING: -h is accepted by login if and only if 1108 * getuid() == 0. 1109 * -p : don't clobber the environment (so terminal type stays set). 1110 * 1111 * -f : force this login, he has already been authenticated 1112 */ 1113 argv = addarg(0, "login"); 1114 1115#if !defined(NO_LOGIN_H) 1116#ifdef AUTHENTICATION 1117# if defined(NO_LOGIN_F) && defined(LOGIN_R) 1118 /* 1119 * Don't add the "-h host" option if we are going 1120 * to be adding the "-r host" option down below... 1121 */ 1122 if ((auth_level < 0) || (autologin != AUTH_VALID)) 1123# endif 1124 { 1125 argv = addarg(argv, "-h"); 1126 argv = addarg(argv, host); 1127 } 1128#endif /* AUTHENTICATION */ 1129#endif 1130#if !defined(NO_LOGIN_P) 1131 argv = addarg(argv, "-p"); 1132#endif 1133#ifdef LINEMODE 1134 /* 1135 * Set the environment variable "LINEMODE" to either 1136 * "real" or "kludge" if we are operating in either 1137 * real or kludge linemode. 1138 */ 1139 if (lmodetype == REAL_LINEMODE) 1140 setenv("LINEMODE", "real", 1); 1141# ifdef KLUDGELINEMODE 1142 else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK) 1143 setenv("LINEMODE", "kludge", 1); 1144# endif 1145#endif 1146#ifdef BFTPDAEMON 1147 /* 1148 * Are we working as the bftp daemon? If so, then ask login 1149 * to start bftp instead of shell. 1150 */ 1151 if (bftpd) { 1152 argv = addarg(argv, "-e"); 1153 argv = addarg(argv, BFTPPATH); 1154 } else 1155#endif 1156#ifdef AUTHENTICATION 1157 if (auth_level >= 0 && autologin == AUTH_VALID) { 1158# if !defined(NO_LOGIN_F) 1159 argv = addarg(argv, "-f"); 1160 argv = addarg(argv, "--"); 1161 argv = addarg(argv, name); 1162# else 1163# if defined(LOGIN_R) 1164 /* 1165 * We don't have support for "login -f", but we 1166 * can fool /bin/login into thinking that we are 1167 * rlogind, and allow us to log in without a 1168 * password. The rlogin protocol expects 1169 * local-user\0remote-user\0term/speed\0 1170 */ 1171 1172 if (pty > 2) { 1173 char *cp; 1174 char speed[128]; 1175 int isecho, israw, xpty, len; 1176 extern int def_rspeed; 1177# ifndef LOGIN_HOST 1178 /* 1179 * Tell login that we are coming from "localhost". 1180 * If we passed in the real host name, then the 1181 * user would have to allow .rhost access from 1182 * every machine that they want authenticated 1183 * access to work from, which sort of defeats 1184 * the purpose of an authenticated login... 1185 * So, we tell login that the session is coming 1186 * from "localhost", and the user will only have 1187 * to have "localhost" in their .rhost file. 1188 */ 1189# define LOGIN_HOST "localhost" 1190# endif 1191 argv = addarg(argv, "-r"); 1192 argv = addarg(argv, LOGIN_HOST); 1193 1194 xpty = pty; 1195 pty = 0; 1196 init_termbuf(); 1197 isecho = tty_isecho(); 1198 israw = tty_israw(); 1199 if (isecho || !israw) { 1200 tty_setecho(0); /* Turn off echo */ 1201 tty_setraw(1); /* Turn on raw */ 1202 set_termbuf(); 1203 } 1204 len = strlen(name)+1; 1205 write(xpty, name, len); 1206 write(xpty, name, len); 1207 snprintf(speed, sizeof(speed), 1208 "%s/%d", (cp = getenv("TERM")) ? cp : "", 1209 (def_rspeed > 0) ? def_rspeed : 9600); 1210 len = strlen(speed)+1; 1211 write(xpty, speed, len); 1212 1213 if (isecho || !israw) { 1214 init_termbuf(); 1215 tty_setecho(isecho); 1216 tty_setraw(israw); 1217 set_termbuf(); 1218 if (!israw) { 1219 /* 1220 * Write a newline to ensure 1221 * that login will be able to 1222 * read the line... 1223 */ 1224 write(xpty, "\n", 1); 1225 } 1226 } 1227 pty = xpty; 1228 } 1229# else 1230 argv = addarg(argv, "--"); 1231 argv = addarg(argv, name); 1232# endif 1233# endif 1234 } else 1235#endif 1236 if (getenv("USER")) { 1237 argv = addarg(argv, "--"); 1238 argv = addarg(argv, getenv("USER")); 1239#if defined(LOGIN_ARGS) && defined(NO_LOGIN_P) 1240 { 1241 char **cpp; 1242 for (cpp = environ; *cpp; cpp++) 1243 argv = addarg(argv, *cpp); 1244 } 1245#endif 1246 /* 1247 * Assume that login will set the USER variable 1248 * correctly. For SysV systems, this means that 1249 * USER will no longer be set, just LOGNAME by 1250 * login. (The problem is that if the auto-login 1251 * fails, and the user then specifies a different 1252 * account name, he can get logged in with both 1253 * LOGNAME and USER in his environment, but the 1254 * USER value will be wrong. 1255 */ 1256 unsetenv("USER"); 1257 } 1258#ifdef AUTHENTICATION 1259#if defined(NO_LOGIN_F) && defined(LOGIN_R) 1260 if (pty > 2) 1261 close(pty); 1262#endif 1263#endif /* AUTHENTICATION */ 1264 closelog(); 1265 1266 if (altlogin == NULL) { 1267 altlogin = _PATH_LOGIN; 1268 } 1269 execv(altlogin, argv); 1270 1271 syslog(LOG_ERR, "%s: %m", altlogin); 1272 fatalperror(net, altlogin); 1273 /*NOTREACHED*/ 1274} 1275 1276static char ** 1277addarg(char **argv, const char *val) 1278{ 1279 char **cpp; 1280 1281 if (argv == NULL) { 1282 /* 1283 * 10 entries, a leading length, and a null 1284 */ 1285 argv = (char **)malloc(sizeof(*argv) * 12); 1286 if (argv == NULL) 1287 return(NULL); 1288 *argv++ = (char *)10; 1289 *argv = (char *)0; 1290 } 1291 for (cpp = argv; *cpp; cpp++) 1292 ; 1293 if (cpp == &argv[(long)argv[-1]]) { 1294 --argv; 1295 *argv = (char *)((long)(*argv) + 10); 1296 argv = (char **)realloc(argv, sizeof(*argv)*((long)(*argv) + 2)); 1297 if (argv == NULL) 1298 return(NULL); 1299 argv++; 1300 cpp = &argv[(long)argv[-1] - 10]; 1301 } 1302 *cpp++ = strdup(val); 1303 *cpp = 0; 1304 return(argv); 1305} 1306 1307/* 1308 * scrub_env() 1309 * 1310 * We only accept the environment variables listed below. 1311 */ 1312void 1313scrub_env(void) 1314{ 1315 static const char *rej[] = { 1316 "TERMCAP=/", 1317 NULL 1318 }; 1319 1320 static const char *acc[] = { 1321 "XAUTH=", "XAUTHORITY=", "DISPLAY=", 1322 "TERM=", 1323 "EDITOR=", 1324 "PAGER=", 1325 "LOGNAME=", 1326 "POSIXLY_CORRECT=", 1327 "PRINTER=", 1328 NULL 1329 }; 1330 1331 char **cpp, **cpp2; 1332 const char **p; 1333 1334 for (cpp2 = cpp = environ; *cpp; cpp++) { 1335 int reject_it = 0; 1336 1337 for(p = rej; *p; p++) 1338 if(strncmp(*cpp, *p, strlen(*p)) == 0) { 1339 reject_it = 1; 1340 break; 1341 } 1342 if (reject_it) 1343 continue; 1344 1345 for(p = acc; *p; p++) 1346 if(strncmp(*cpp, *p, strlen(*p)) == 0) 1347 break; 1348 if(*p != NULL) 1349 *cpp2++ = *cpp; 1350 } 1351 *cpp2 = NULL; 1352} 1353 1354/* 1355 * cleanup() 1356 * 1357 * This is the routine to call when we are all through, to 1358 * clean up anything that needs to be cleaned up. 1359 */ 1360/* ARGSUSED */ 1361void 1362cleanup(int sig __unused) 1363{ 1364 char *p; 1365 sigset_t mask; 1366 1367 p = line + sizeof(_PATH_DEV) - 1; 1368 /* 1369 * Block all signals before clearing the utmp entry. We don't want to 1370 * be called again after calling logout() and then not add the wtmp 1371 * entry because of not finding the corresponding entry in utmp. 1372 */ 1373 sigfillset(&mask); 1374 sigprocmask(SIG_SETMASK, &mask, NULL); 1375#if (!defined(__BEOS__) && !defined(__HAIKU__)) 1376 if (logout(p)) 1377 logwtmp(p, "", ""); 1378 (void)chmod(line, 0666); 1379 (void)chown(line, 0, 0); 1380 *p = 'p'; 1381 (void)chmod(line, 0666); 1382 (void)chown(line, 0, 0); 1383#endif 1384 (void) shutdown(net, 2); 1385 _exit(1); 1386} 1387