No empty .Rs/.Re
[netbsd-mini2440.git] / libexec / telnetd / sys_term.c
blob38f4058326f6fc495b75b0c69e4ff6ad28843eb6
1 /* $NetBSD: sys_term.c,v 1.43 2005/05/05 01:28:57 lukem Exp $ */
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)sys_term.c 8.4+1 (Berkeley) 5/30/95";
36 #else
37 __RCSID("$NetBSD: sys_term.c,v 1.43 2005/05/05 01:28:57 lukem Exp $");
38 #endif
39 #endif /* not lint */
41 #include "telnetd.h"
42 #include "pathnames.h"
44 #include <util.h>
45 #include <vis.h>
47 #include <utmp.h>
48 struct utmp wtmp;
50 #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
51 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
53 struct termios termbuf, termbuf2; /* pty control structure */
55 void getptyslave(void);
56 int cleanopen(char *);
57 char **addarg(char **, char *);
58 void scrub_env(void);
59 int getent(char *, char *);
60 char *getstr(const char *, char **);
61 #ifdef KRB5
62 extern void kerberos5_cleanup(void);
63 #endif
66 * init_termbuf()
67 * copy_termbuf(cp)
68 * set_termbuf()
70 * These three routines are used to get and set the "termbuf" structure
71 * to and from the kernel. init_termbuf() gets the current settings.
72 * copy_termbuf() hands in a new "termbuf" to write to the kernel, and
73 * set_termbuf() writes the structure into the kernel.
76 void
77 init_termbuf(void)
79 (void) tcgetattr(pty, &termbuf);
80 termbuf2 = termbuf;
83 #if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
84 void
85 copy_termbuf(char *cp, int len)
87 if (len > sizeof(termbuf))
88 len = sizeof(termbuf);
89 memmove((char *)&termbuf, cp, len);
90 termbuf2 = termbuf;
92 #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
94 void
95 set_termbuf(void)
98 * Only make the necessary changes.
100 if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
101 (void) tcsetattr(pty, TCSANOW, &termbuf);
106 * spcset(func, valp, valpp)
108 * This function takes various special characters (func), and
109 * sets *valp to the current value of that character, and
110 * *valpp to point to where in the "termbuf" structure that
111 * value is kept.
113 * It returns the SLC_ level of support for this function.
118 spcset(int func, cc_t *valp, cc_t **valpp)
121 #define setval(a, b) *valp = termbuf.c_cc[a]; \
122 *valpp = &termbuf.c_cc[a]; \
123 return(b);
124 #define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT);
126 switch(func) {
127 case SLC_EOF:
128 setval(VEOF, SLC_VARIABLE);
129 case SLC_EC:
130 setval(VERASE, SLC_VARIABLE);
131 case SLC_EL:
132 setval(VKILL, SLC_VARIABLE);
133 case SLC_IP:
134 setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
135 case SLC_ABORT:
136 setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
137 case SLC_XON:
138 setval(VSTART, SLC_VARIABLE);
139 case SLC_XOFF:
140 setval(VSTOP, SLC_VARIABLE);
141 case SLC_EW:
142 setval(VWERASE, SLC_VARIABLE);
143 case SLC_RP:
144 setval(VREPRINT, SLC_VARIABLE);
145 case SLC_LNEXT:
146 setval(VLNEXT, SLC_VARIABLE);
147 case SLC_AO:
148 setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
149 case SLC_SUSP:
150 setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
151 case SLC_FORW1:
152 setval(VEOL, SLC_VARIABLE);
153 case SLC_FORW2:
154 setval(VEOL2, SLC_VARIABLE);
155 case SLC_AYT:
156 setval(VSTATUS, SLC_VARIABLE);
158 case SLC_BRK:
159 case SLC_SYNCH:
160 case SLC_EOR:
161 defval(0);
163 default:
164 *valp = 0;
165 *valpp = 0;
166 return(SLC_NOSUPPORT);
172 * getpty()
174 * Allocate a pty. As a side effect, the external character
175 * array "line" contains the name of the slave side.
177 * Returns the file descriptor of the opened pty.
179 #ifndef __GNUC__
180 char *line = NULL16STR;
181 #else
182 static char Xline[] = NULL16STR;
183 char *line = Xline;
184 #endif
187 static int ptyslavefd; /* for cleanopen() */
190 getpty(int *ptynum)
192 int ptyfd;
194 ptyfd = openpty(ptynum, &ptyslavefd, line, NULL, NULL);
195 if (ptyfd == 0)
196 return *ptynum;
197 ptyslavefd = -1;
198 return (-1);
201 #ifdef LINEMODE
203 * tty_flowmode() Find out if flow control is enabled or disabled.
204 * tty_linemode() Find out if linemode (external processing) is enabled.
205 * tty_setlinemod(on) Turn on/off linemode.
206 * tty_isecho() Find out if echoing is turned on.
207 * tty_setecho(on) Enable/disable character echoing.
208 * tty_israw() Find out if terminal is in RAW mode.
209 * tty_binaryin(on) Turn on/off BINARY on input.
210 * tty_binaryout(on) Turn on/off BINARY on output.
211 * tty_isediting() Find out if line editing is enabled.
212 * tty_istrapsig() Find out if signal trapping is enabled.
213 * tty_setedit(on) Turn on/off line editing.
214 * tty_setsig(on) Turn on/off signal trapping.
215 * tty_issofttab() Find out if tab expansion is enabled.
216 * tty_setsofttab(on) Turn on/off soft tab expansion.
217 * tty_islitecho() Find out if typed control chars are echoed literally
218 * tty_setlitecho() Turn on/off literal echo of control chars
219 * tty_tspeed(val) Set transmit speed to val.
220 * tty_rspeed(val) Set receive speed to val.
225 tty_linemode(void)
227 return(termbuf.c_lflag & EXTPROC);
230 void
231 tty_setlinemode(int on)
233 set_termbuf();
234 (void) ioctl(pty, TIOCEXT, (char *)&on);
235 init_termbuf();
237 #endif /* LINEMODE */
240 tty_isecho(void)
242 return (termbuf.c_lflag & ECHO);
246 tty_flowmode(void)
248 return((termbuf.c_iflag & IXON) ? 1 : 0);
252 tty_restartany(void)
254 return((termbuf.c_iflag & IXANY) ? 1 : 0);
257 void
258 tty_setecho(int on)
260 if (on)
261 termbuf.c_lflag |= ECHO;
262 else
263 termbuf.c_lflag &= ~ECHO;
267 tty_israw(void)
269 return(!(termbuf.c_lflag & ICANON));
272 void
273 tty_binaryin(int on)
275 if (on) {
276 termbuf.c_iflag &= ~ISTRIP;
277 } else {
278 termbuf.c_iflag |= ISTRIP;
282 void
283 tty_binaryout(int on)
285 if (on) {
286 termbuf.c_cflag &= ~(CSIZE|PARENB);
287 termbuf.c_cflag |= CS8;
288 termbuf.c_oflag &= ~OPOST;
289 } else {
290 termbuf.c_cflag &= ~CSIZE;
291 termbuf.c_cflag |= CS7|PARENB;
292 termbuf.c_oflag |= OPOST;
297 tty_isbinaryin(void)
299 return(!(termbuf.c_iflag & ISTRIP));
303 tty_isbinaryout(void)
305 return(!(termbuf.c_oflag&OPOST));
308 #ifdef LINEMODE
310 tty_isediting(void)
312 return(termbuf.c_lflag & ICANON);
316 tty_istrapsig(void)
318 return(termbuf.c_lflag & ISIG);
321 void
322 tty_setedit(int on)
324 if (on)
325 termbuf.c_lflag |= ICANON;
326 else
327 termbuf.c_lflag &= ~ICANON;
330 void
331 tty_setsig(int on)
333 if (on)
334 termbuf.c_lflag |= ISIG;
335 else
336 termbuf.c_lflag &= ~ISIG;
338 #endif /* LINEMODE */
341 tty_issofttab(void)
343 # ifdef OXTABS
344 return (termbuf.c_oflag & OXTABS);
345 # endif
346 # ifdef TABDLY
347 return ((termbuf.c_oflag & TABDLY) == TAB3);
348 # endif
351 void
352 tty_setsofttab(int on)
354 if (on) {
355 # ifdef OXTABS
356 termbuf.c_oflag |= OXTABS;
357 # endif
358 # ifdef TABDLY
359 termbuf.c_oflag &= ~TABDLY;
360 termbuf.c_oflag |= TAB3;
361 # endif
362 } else {
363 # ifdef OXTABS
364 termbuf.c_oflag &= ~OXTABS;
365 # endif
366 # ifdef TABDLY
367 termbuf.c_oflag &= ~TABDLY;
368 termbuf.c_oflag |= TAB0;
369 # endif
374 tty_islitecho(void)
376 # ifdef ECHOCTL
377 return (!(termbuf.c_lflag & ECHOCTL));
378 # endif
379 # ifdef TCTLECH
380 return (!(termbuf.c_lflag & TCTLECH));
381 # endif
382 # if !defined(ECHOCTL) && !defined(TCTLECH)
383 return (0); /* assumes ctl chars are echoed '^x' */
384 # endif
387 void
388 tty_setlitecho(int on)
390 # ifdef ECHOCTL
391 if (on)
392 termbuf.c_lflag &= ~ECHOCTL;
393 else
394 termbuf.c_lflag |= ECHOCTL;
395 # endif
396 # ifdef TCTLECH
397 if (on)
398 termbuf.c_lflag &= ~TCTLECH;
399 else
400 termbuf.c_lflag |= TCTLECH;
401 # endif
405 tty_iscrnl(void)
407 return (termbuf.c_iflag & ICRNL);
410 void
411 tty_tspeed(int val)
413 cfsetospeed(&termbuf, val);
416 void
417 tty_rspeed(int val)
419 cfsetispeed(&termbuf, val);
426 * getptyslave()
428 * Open the slave side of the pty, and do any initialization
429 * that is necessary. The return value is a file descriptor
430 * for the slave side.
432 extern int def_tspeed, def_rspeed;
433 extern int def_row, def_col;
435 void
436 getptyslave(void)
438 int t = -1;
440 #ifdef LINEMODE
441 int waslm;
442 #endif
443 struct winsize ws;
445 * Opening the slave side may cause initilization of the
446 * kernel tty structure. We need remember the state of
447 * if linemode was turned on
448 * terminal window size
449 * terminal speed
450 * so that we can re-set them if we need to.
452 #ifdef LINEMODE
453 waslm = tty_linemode();
454 #endif
457 * Make sure that we don't have a controlling tty, and
458 * that we are the session (process group) leader.
460 t = open(_PATH_TTY, O_RDWR);
461 if (t >= 0) {
462 (void) ioctl(t, TIOCNOTTY, (char *)0);
463 (void) close(t);
468 t = cleanopen(line);
469 if (t < 0)
470 fatalperror(net, line);
474 * set up the tty modes as we like them to be.
476 init_termbuf();
477 if (def_row || def_col) {
478 memset((char *)&ws, 0, sizeof(ws));
479 ws.ws_col = def_col;
480 ws.ws_row = def_row;
481 (void)ioctl(t, TIOCSWINSZ, (char *)&ws);
485 * Settings for sgtty based systems
489 * Settings for all other termios/termio based
490 * systems, other than 4.4BSD. In 4.4BSD the
491 * kernel does the initial terminal setup.
493 tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
494 tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
495 #ifdef LINEMODE
496 if (waslm)
497 tty_setlinemode(1);
498 #endif /* LINEMODE */
501 * Set the tty modes, and make this our controlling tty.
503 set_termbuf();
504 if (login_tty(t) == -1)
505 fatalperror(net, "login_tty");
506 if (net > 2)
507 (void) close(net);
508 if (pty > 2) {
509 (void) close(pty);
510 pty = -1;
515 * Open the specified slave side of the pty,
516 * making sure that we have a clean tty.
519 cleanopen(char *ttyline)
521 return ptyslavefd;
525 * startslave(host)
527 * Given a hostname, do whatever
528 * is necessary to startup the login process on the slave side of the pty.
531 /* ARGSUSED */
532 void
533 startslave(char *host, int autologin, char *autoname)
535 int i;
537 #ifdef AUTHENTICATION
538 if (!autoname || !autoname[0])
539 autologin = 0;
541 if (autologin < auth_level) {
542 fatal(net, "Authorization failed");
543 exit(1);
545 #endif
548 if ((i = fork()) < 0)
549 fatalperror(net, "fork");
550 if (i) {
551 } else {
552 getptyslave();
553 start_login(host, autologin, autoname);
554 /*NOTREACHED*/
558 char *envinit[3];
560 void
561 init_env(void)
563 char **envp;
565 envp = envinit;
566 if ((*envp = getenv("TZ")))
567 *envp++ -= 3;
568 *envp = 0;
569 environ = envinit;
574 * start_login(host)
576 * Assuming that we are now running as a child processes, this
577 * function will turn us into the login process.
579 extern char *gettyname;
581 void
582 start_login(char *host, int autologin, char *name)
584 char **argv;
585 #define TABBUFSIZ 512
586 char defent[TABBUFSIZ];
587 char defstrs[TABBUFSIZ];
588 #undef TABBUFSIZ
589 const char *loginprog = NULL;
590 extern struct sockaddr_storage from;
591 char buf[sizeof(from) * 4 + 1];
593 scrub_env();
596 * -a : pass on the address of the host.
597 * -h : pass on name of host.
598 * WARNING: -h and -a are accepted by login
599 * if and only if getuid() == 0.
600 * -p : don't clobber the environment (so terminal type stays set).
602 * -f : force this login, he has already been authenticated
604 argv = addarg(0, "login");
606 argv = addarg(argv, "-a");
607 (void)strvisx(buf, (const char *)(const void *)&from, sizeof(from),
608 VIS_WHITE);
609 argv = addarg(argv, buf);
611 argv = addarg(argv, "-h");
612 argv = addarg(argv, host);
614 argv = addarg(argv, "-p");
615 #ifdef LINEMODE
617 * Set the environment variable "LINEMODE" to either
618 * "real" or "kludge" if we are operating in either
619 * real or kludge linemode.
621 if (lmodetype == REAL_LINEMODE)
622 setenv("LINEMODE", "real", 1);
623 # ifdef KLUDGELINEMODE
624 else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK)
625 setenv("LINEMODE", "kludge", 1);
626 # endif
627 #endif
628 #ifdef SECURELOGIN
630 * don't worry about the -f that might get sent.
631 * A -s is supposed to override it anyhow.
633 if (require_secure_login)
634 argv = addarg(argv, "-s");
635 #endif
636 #ifdef AUTHENTICATION
637 if (auth_level >= 0 && autologin == AUTH_VALID) {
638 argv = addarg(argv, "-f");
639 argv = addarg(argv, "--");
640 argv = addarg(argv, name);
641 } else
642 #endif
643 if (getenv("USER")) {
644 argv = addarg(argv, "--");
645 argv = addarg(argv, getenv("USER"));
647 * Assume that login will set the USER variable
648 * correctly. For SysV systems, this means that
649 * USER will no longer be set, just LOGNAME by
650 * login. (The problem is that if the auto-login
651 * fails, and the user then specifies a different
652 * account name, he can get logged in with both
653 * LOGNAME and USER in his environment, but the
654 * USER value will be wrong.
656 unsetenv("USER");
658 if (getent(defent, gettyname) == 1) {
659 char *cp = defstrs;
661 loginprog = getstr("lo", &cp);
663 if (loginprog == NULL)
664 loginprog = _PATH_LOGIN;
665 closelog();
667 * This sleep(1) is in here so that telnetd can
668 * finish up with the tty. There's a race condition
669 * the login banner message gets lost...
671 sleep(1);
672 execv(loginprog, argv);
674 syslog(LOG_ERR, "%s: %m", loginprog);
675 fatalperror(net, loginprog);
676 /*NOTREACHED*/
679 char **
680 addarg(char **argv, char *val)
682 char **cpp;
683 char **nargv;
685 if (argv == NULL) {
687 * 10 entries, a leading length, and a null
689 argv = (char **)malloc(sizeof(*argv) * 12);
690 if (argv == NULL)
691 return(NULL);
692 *argv++ = (char *)10;
693 *argv = (char *)0;
695 for (cpp = argv; *cpp; cpp++)
697 if (cpp == &argv[(long)argv[-1]]) {
698 --argv;
699 nargv = (char **)realloc(argv,
700 sizeof(*argv) * ((long)(*argv) + 10 + 2));
701 if (argv == NULL) {
702 fatal(net, "not enough memory");
703 /*NOTREACHED*/
705 argv = nargv;
706 *argv = (char *)((long)(*argv) + 10);
707 argv++;
708 cpp = &argv[(long)argv[-1] - 10];
710 *cpp++ = val;
711 *cpp = 0;
712 return(argv);
716 * scrub_env()
718 * We only accept the environment variables listed below.
721 void
722 scrub_env(void)
724 static const char *reject[] = {
725 "TERMCAP=/",
726 NULL
729 static const char *acceptstr[] = {
730 "XAUTH=", "XAUTHORITY=", "DISPLAY=",
731 "TERM=",
732 "EDITOR=",
733 "PAGER=",
734 "LOGNAME=",
735 "POSIXLY_CORRECT=",
736 "TERMCAP=",
737 "PRINTER=",
738 NULL
741 char **cpp, **cpp2;
742 const char **p;
744 for (cpp2 = cpp = environ; *cpp; cpp++) {
745 int reject_it = 0;
747 for(p = reject; *p; p++)
748 if(strncmp(*cpp, *p, strlen(*p)) == 0) {
749 reject_it = 1;
750 break;
752 if (reject_it)
753 continue;
755 for(p = acceptstr; *p; p++)
756 if(strncmp(*cpp, *p, strlen(*p)) == 0)
757 break;
758 if(*p != NULL)
759 *cpp2++ = *cpp;
761 *cpp2 = NULL;
765 * cleanup()
767 * This is the routine to call when we are all through, to
768 * clean up anything that needs to be cleaned up.
770 /* ARGSUSED */
771 void
772 cleanup(int sig)
774 char *p, c;
776 p = line + sizeof(_PATH_DEV) - 1;
777 #ifdef SUPPORT_UTMP
778 if (logout(p))
779 logwtmp(p, "", "");
780 #endif
781 #ifdef SUPPORT_UTMPX
782 if (logoutx(p, 0, DEAD_PROCESS))
783 logwtmpx(p, "", "", 0, DEAD_PROCESS);
784 #endif
785 (void)chmod(line, 0666);
786 (void)chown(line, 0, 0);
787 c = *p; *p = 'p';
788 (void)chmod(line, 0666);
789 (void)chown(line, 0, 0);
790 *p = c;
791 if (ttyaction(line, "telnetd", "root"))
792 syslog(LOG_ERR, "%s: ttyaction failed", line);
793 (void) shutdown(net, 2);
794 exit(1);