telnetd.c revision 72445
157416Smarkm/* 257416Smarkm * Copyright (c) 1989, 1993 357416Smarkm * The Regents of the University of California. All rights reserved. 457416Smarkm * 557416Smarkm * Redistribution and use in source and binary forms, with or without 657416Smarkm * modification, are permitted provided that the following conditions 757416Smarkm * are met: 857416Smarkm * 1. Redistributions of source code must retain the above copyright 957416Smarkm * notice, this list of conditions and the following disclaimer. 1057416Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1157416Smarkm * notice, this list of conditions and the following disclaimer in the 1257416Smarkm * documentation and/or other materials provided with the distribution. 1357416Smarkm * 3. All advertising materials mentioning features or use of this software 1457416Smarkm * must display the following acknowledgement: 1557416Smarkm * This product includes software developed by the University of 1657416Smarkm * California, Berkeley and its contributors. 1757416Smarkm * 4. Neither the name of the University nor the names of its contributors 1857416Smarkm * may be used to endorse or promote products derived from this software 1957416Smarkm * without specific prior written permission. 2057416Smarkm * 2157416Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2257416Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2357416Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2457416Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2557416Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2657416Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2757416Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2857416Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2957416Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3057416Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3157416Smarkm * SUCH DAMAGE. 3257416Smarkm */ 3357416Smarkm 3457416Smarkm#include "telnetd.h" 3557416Smarkm 3672445SassarRCSID("$Id: telnetd.c,v 1.63 2000/10/08 13:32:28 assar Exp $"); 3757416Smarkm 3857416Smarkm#ifdef _SC_CRAY_SECURE_SYS 3957416Smarkm#include <sys/sysv.h> 4057416Smarkm#include <sys/secdev.h> 4157416Smarkm#include <sys/secparm.h> 4257416Smarkm#include <sys/usrv.h> 4357416Smarkmint secflag; 4457416Smarkmchar tty_dev[16]; 4557416Smarkmstruct secdev dv; 4657416Smarkmstruct sysv sysv; 4757416Smarkmstruct socksec ss; 4857416Smarkm#endif /* _SC_CRAY_SECURE_SYS */ 4957416Smarkm 5057416Smarkm#ifdef AUTHENTICATION 5157416Smarkmint auth_level = 0; 5257416Smarkm#endif 5357416Smarkm 5457416Smarkmextern int utmp_len; 5557416Smarkmint registerd_host_only = 0; 5657416Smarkm 5757416Smarkm#ifdef STREAMSPTY 5857416Smarkm# include <stropts.h> 5957416Smarkm# include <termios.h> 6057416Smarkm#ifdef HAVE_SYS_UIO_H 6157416Smarkm#include <sys/uio.h> 6257416Smarkm#endif /* HAVE_SYS_UIO_H */ 6357416Smarkm#ifdef HAVE_SYS_STREAM_H 6457416Smarkm#include <sys/stream.h> 6557416Smarkm#endif 6657416Smarkm#ifdef _AIX 6757416Smarkm#include <sys/termio.h> 6857416Smarkm#endif 6957416Smarkm# ifdef HAVE_SYS_STRTTY_H 7057416Smarkm# include <sys/strtty.h> 7157416Smarkm# endif 7257416Smarkm# ifdef HAVE_SYS_STR_TTY_H 7357416Smarkm# include <sys/str_tty.h> 7457416Smarkm# endif 7557416Smarkm/* make sure we don't get the bsd version */ 7657416Smarkm/* what is this here for? solaris? /joda */ 7757416Smarkm# ifdef HAVE_SYS_TTY_H 7857416Smarkm# include "/usr/include/sys/tty.h" 7957416Smarkm# endif 8057416Smarkm# ifdef HAVE_SYS_PTYVAR_H 8157416Smarkm# include <sys/ptyvar.h> 8257416Smarkm# endif 8357416Smarkm 8457416Smarkm/* 8557416Smarkm * Because of the way ptyibuf is used with streams messages, we need 8657416Smarkm * ptyibuf+1 to be on a full-word boundary. The following wierdness 8757416Smarkm * is simply to make that happen. 8857416Smarkm */ 8957416Smarkmlong ptyibufbuf[BUFSIZ/sizeof(long)+1]; 9057416Smarkmchar *ptyibuf = ((char *)&ptyibufbuf[1])-1; 9157416Smarkmchar *ptyip = ((char *)&ptyibufbuf[1])-1; 9257416Smarkmchar ptyibuf2[BUFSIZ]; 9357416Smarkmunsigned char ctlbuf[BUFSIZ]; 9457416Smarkmstruct strbuf strbufc, strbufd; 9557416Smarkm 9657416Smarkmint readstream(int, char*, int); 9757416Smarkm 9857416Smarkm#else /* ! STREAMPTY */ 9957416Smarkm 10057416Smarkm/* 10157416Smarkm * I/O data buffers, 10257416Smarkm * pointers, and counters. 10357416Smarkm */ 10457416Smarkmchar ptyibuf[BUFSIZ], *ptyip = ptyibuf; 10557416Smarkmchar ptyibuf2[BUFSIZ]; 10657416Smarkm 10757416Smarkm#endif /* ! STREAMPTY */ 10857416Smarkm 10957416Smarkmint hostinfo = 1; /* do we print login banner? */ 11057416Smarkm 11157416Smarkm#ifdef _CRAY 11257416Smarkmextern int newmap; /* nonzero if \n maps to ^M^J */ 11357416Smarkmint lowpty = 0, highpty; /* low, high pty numbers */ 11457416Smarkm#endif /* CRAY */ 11557416Smarkm 11657416Smarkmint debug = 0; 11757416Smarkmint keepalive = 1; 11857416Smarkmchar *progname; 11957416Smarkm 12057416Smarkmstatic void usage (void); 12157416Smarkm 12257416Smarkm/* 12357416Smarkm * The string to pass to getopt(). We do it this way so 12457416Smarkm * that only the actual options that we support will be 12557416Smarkm * passed off to getopt(). 12657416Smarkm */ 12757416Smarkmchar valid_opts[] = "Bd:hklnS:u:UL:y" 12857416Smarkm#ifdef AUTHENTICATION 12957416Smarkm "a:X:z" 13057416Smarkm#endif 13157416Smarkm#ifdef DIAGNOSTICS 13257416Smarkm "D:" 13357416Smarkm#endif 13457416Smarkm#ifdef _CRAY 13557416Smarkm "r:" 13657416Smarkm#endif 13757416Smarkm ; 13857416Smarkm 13957416Smarkmstatic void doit(struct sockaddr*, int); 14057416Smarkm 14157416Smarkmint 14257416Smarkmmain(int argc, char **argv) 14357416Smarkm{ 14457416Smarkm struct sockaddr_storage __ss; 14557416Smarkm struct sockaddr *sa = (struct sockaddr *)&__ss; 14672445Sassar int on = 1; 14772445Sassar socklen_t sa_size; 14857416Smarkm int ch; 14957416Smarkm#if defined(IPPROTO_IP) && defined(IP_TOS) 15057416Smarkm int tos = -1; 15157416Smarkm#endif 15257416Smarkm#ifdef ENCRYPTION 15357416Smarkm extern int des_check_key; 15457416Smarkm des_check_key = 1; /* Kludge for Mac NCSA telnet 2.6 /bg */ 15557416Smarkm#endif 15657416Smarkm pfrontp = pbackp = ptyobuf; 15757416Smarkm netip = netibuf; 15857416Smarkm nfrontp = nbackp = netobuf; 15957416Smarkm 16057416Smarkm progname = *argv; 16157416Smarkm#ifdef ENCRYPTION 16257416Smarkm nclearto = 0; 16357416Smarkm#endif 16457416Smarkm 16557416Smarkm#ifdef _CRAY 16657416Smarkm /* 16757416Smarkm * Get number of pty's before trying to process options, 16857416Smarkm * which may include changing pty range. 16957416Smarkm */ 17057416Smarkm highpty = getnpty(); 17157416Smarkm#endif /* CRAY */ 17257416Smarkm 17357416Smarkm while ((ch = getopt(argc, argv, valid_opts)) != -1) { 17457416Smarkm switch(ch) { 17557416Smarkm 17657416Smarkm#ifdef AUTHENTICATION 17757416Smarkm case 'a': 17857416Smarkm /* 17957416Smarkm * Check for required authentication level 18057416Smarkm */ 18157416Smarkm if (strcmp(optarg, "debug") == 0) { 18257416Smarkm auth_debug_mode = 1; 18357416Smarkm } else if (strcasecmp(optarg, "none") == 0) { 18457416Smarkm auth_level = 0; 18557416Smarkm } else if (strcasecmp(optarg, "otp") == 0) { 18657416Smarkm auth_level = 0; 18757416Smarkm require_otp = 1; 18857416Smarkm } else if (strcasecmp(optarg, "other") == 0) { 18957416Smarkm auth_level = AUTH_OTHER; 19057416Smarkm } else if (strcasecmp(optarg, "user") == 0) { 19157416Smarkm auth_level = AUTH_USER; 19257416Smarkm } else if (strcasecmp(optarg, "valid") == 0) { 19357416Smarkm auth_level = AUTH_VALID; 19457416Smarkm } else if (strcasecmp(optarg, "off") == 0) { 19557416Smarkm /* 19657416Smarkm * This hack turns off authentication 19757416Smarkm */ 19857416Smarkm auth_level = -1; 19957416Smarkm } else { 20057416Smarkm fprintf(stderr, 20157416Smarkm "telnetd: unknown authorization level for -a\n"); 20257416Smarkm } 20357416Smarkm break; 20457416Smarkm#endif /* AUTHENTICATION */ 20557416Smarkm 20657416Smarkm case 'B': /* BFTP mode is not supported any more */ 20757416Smarkm break; 20857416Smarkm case 'd': 20957416Smarkm if (strcmp(optarg, "ebug") == 0) { 21057416Smarkm debug++; 21157416Smarkm break; 21257416Smarkm } 21357416Smarkm usage(); 21457416Smarkm /* NOTREACHED */ 21557416Smarkm break; 21657416Smarkm 21757416Smarkm#ifdef DIAGNOSTICS 21857416Smarkm case 'D': 21957416Smarkm /* 22057416Smarkm * Check for desired diagnostics capabilities. 22157416Smarkm */ 22257416Smarkm if (!strcmp(optarg, "report")) { 22357416Smarkm diagnostic |= TD_REPORT|TD_OPTIONS; 22457416Smarkm } else if (!strcmp(optarg, "exercise")) { 22557416Smarkm diagnostic |= TD_EXERCISE; 22657416Smarkm } else if (!strcmp(optarg, "netdata")) { 22757416Smarkm diagnostic |= TD_NETDATA; 22857416Smarkm } else if (!strcmp(optarg, "ptydata")) { 22957416Smarkm diagnostic |= TD_PTYDATA; 23057416Smarkm } else if (!strcmp(optarg, "options")) { 23157416Smarkm diagnostic |= TD_OPTIONS; 23257416Smarkm } else { 23357416Smarkm usage(); 23457416Smarkm /* NOT REACHED */ 23557416Smarkm } 23657416Smarkm break; 23757416Smarkm#endif /* DIAGNOSTICS */ 23857416Smarkm 23957416Smarkm 24057416Smarkm case 'h': 24157416Smarkm hostinfo = 0; 24257416Smarkm break; 24357416Smarkm 24457416Smarkm case 'k': /* Linemode is not supported any more */ 24557416Smarkm case 'l': 24657416Smarkm break; 24757416Smarkm 24857416Smarkm case 'n': 24957416Smarkm keepalive = 0; 25057416Smarkm break; 25157416Smarkm 25257416Smarkm#ifdef _CRAY 25357416Smarkm case 'r': 25457416Smarkm { 25557416Smarkm char *strchr(); 25657416Smarkm char *c; 25757416Smarkm 25857416Smarkm /* 25957416Smarkm * Allow the specification of alterations 26057416Smarkm * to the pty search range. It is legal to 26157416Smarkm * specify only one, and not change the 26257416Smarkm * other from its default. 26357416Smarkm */ 26457416Smarkm c = strchr(optarg, '-'); 26557416Smarkm if (c) { 26657416Smarkm *c++ = '\0'; 26757416Smarkm highpty = atoi(c); 26857416Smarkm } 26957416Smarkm if (*optarg != '\0') 27057416Smarkm lowpty = atoi(optarg); 27157416Smarkm if ((lowpty > highpty) || (lowpty < 0) || 27257416Smarkm (highpty > 32767)) { 27357416Smarkm usage(); 27457416Smarkm /* NOT REACHED */ 27557416Smarkm } 27657416Smarkm break; 27757416Smarkm } 27857416Smarkm#endif /* CRAY */ 27957416Smarkm 28057416Smarkm case 'S': 28157416Smarkm#ifdef HAVE_PARSETOS 28257416Smarkm if ((tos = parsetos(optarg, "tcp")) < 0) 28357416Smarkm fprintf(stderr, "%s%s%s\n", 28457416Smarkm "telnetd: Bad TOS argument '", optarg, 28557416Smarkm "'; will try to use default TOS"); 28657416Smarkm#else 28757416Smarkm fprintf(stderr, "%s%s\n", "TOS option unavailable; ", 28857416Smarkm "-S flag not supported\n"); 28957416Smarkm#endif 29057416Smarkm break; 29157416Smarkm 29257416Smarkm case 'u': 29357416Smarkm utmp_len = atoi(optarg); 29457416Smarkm break; 29557416Smarkm 29657416Smarkm case 'U': 29757416Smarkm registerd_host_only = 1; 29857416Smarkm break; 29957416Smarkm 30057416Smarkm#ifdef AUTHENTICATION 30157416Smarkm case 'X': 30257416Smarkm /* 30357416Smarkm * Check for invalid authentication types 30457416Smarkm */ 30557416Smarkm auth_disable_name(optarg); 30657416Smarkm break; 30757416Smarkm#endif 30857416Smarkm case 'y': 30957416Smarkm no_warn = 1; 31057416Smarkm break; 31157416Smarkm#ifdef AUTHENTICATION 31257416Smarkm case 'z': 31357416Smarkm log_unauth = 1; 31457416Smarkm break; 31557416Smarkm 31657416Smarkm#endif /* AUTHENTICATION */ 31757416Smarkm 31857416Smarkm case 'L': 31957416Smarkm new_login = optarg; 32057416Smarkm break; 32157416Smarkm 32257416Smarkm default: 32357416Smarkm fprintf(stderr, "telnetd: %c: unknown option\n", ch); 32457416Smarkm /* FALLTHROUGH */ 32557416Smarkm case '?': 32657416Smarkm usage(); 32757416Smarkm /* NOTREACHED */ 32857416Smarkm } 32957416Smarkm } 33057416Smarkm 33157416Smarkm argc -= optind; 33257416Smarkm argv += optind; 33357416Smarkm 33457416Smarkm if (debug) { 33557416Smarkm int port = 0; 33657416Smarkm struct servent *sp; 33757416Smarkm 33857416Smarkm if (argc > 1) { 33957416Smarkm usage (); 34057416Smarkm } else if (argc == 1) { 34157416Smarkm sp = roken_getservbyname (*argv, "tcp"); 34257416Smarkm if (sp) 34357416Smarkm port = sp->s_port; 34457416Smarkm else 34557416Smarkm port = htons(atoi(*argv)); 34657416Smarkm } else { 34757416Smarkm#ifdef KRB5 34857416Smarkm port = krb5_getportbyname (NULL, "telnet", "tcp", 23); 34957416Smarkm#else 35057416Smarkm port = k_getportbyname("telnet", "tcp", htons(23)); 35157416Smarkm#endif 35257416Smarkm } 35357416Smarkm mini_inetd (port); 35457416Smarkm } else if (argc > 0) { 35557416Smarkm usage(); 35657416Smarkm /* NOT REACHED */ 35757416Smarkm } 35857416Smarkm 35957416Smarkm#ifdef _SC_CRAY_SECURE_SYS 36057416Smarkm secflag = sysconf(_SC_CRAY_SECURE_SYS); 36157416Smarkm 36257416Smarkm /* 36357416Smarkm * Get socket's security label 36457416Smarkm */ 36557416Smarkm if (secflag) { 36672445Sassar socklen_t szss = sizeof(ss); 36757416Smarkm int sock_multi; 36872445Sassar socklen_t szi = sizeof(int); 36957416Smarkm 37057416Smarkm memset(&dv, 0, sizeof(dv)); 37157416Smarkm 37257416Smarkm if (getsysv(&sysv, sizeof(struct sysv)) != 0) 37357416Smarkm fatalperror(net, "getsysv"); 37457416Smarkm 37557416Smarkm /* 37657416Smarkm * Get socket security label and set device values 37757416Smarkm * {security label to be set on ttyp device} 37857416Smarkm */ 37957416Smarkm#ifdef SO_SEC_MULTI /* 8.0 code */ 38057416Smarkm if ((getsockopt(0, SOL_SOCKET, SO_SECURITY, 38157416Smarkm (void *)&ss, &szss) < 0) || 38257416Smarkm (getsockopt(0, SOL_SOCKET, SO_SEC_MULTI, 38357416Smarkm (void *)&sock_multi, &szi) < 0)) 38457416Smarkm fatalperror(net, "getsockopt"); 38557416Smarkm else { 38657416Smarkm dv.dv_actlvl = ss.ss_actlabel.lt_level; 38757416Smarkm dv.dv_actcmp = ss.ss_actlabel.lt_compart; 38857416Smarkm if (!sock_multi) { 38957416Smarkm dv.dv_minlvl = dv.dv_maxlvl = dv.dv_actlvl; 39057416Smarkm dv.dv_valcmp = dv.dv_actcmp; 39157416Smarkm } else { 39257416Smarkm dv.dv_minlvl = ss.ss_minlabel.lt_level; 39357416Smarkm dv.dv_maxlvl = ss.ss_maxlabel.lt_level; 39457416Smarkm dv.dv_valcmp = ss.ss_maxlabel.lt_compart; 39557416Smarkm } 39657416Smarkm dv.dv_devflg = 0; 39757416Smarkm } 39857416Smarkm#else /* SO_SEC_MULTI */ /* 7.0 code */ 39957416Smarkm if (getsockopt(0, SOL_SOCKET, SO_SECURITY, 40057416Smarkm (void *)&ss, &szss) >= 0) { 40157416Smarkm dv.dv_actlvl = ss.ss_slevel; 40257416Smarkm dv.dv_actcmp = ss.ss_compart; 40357416Smarkm dv.dv_minlvl = ss.ss_minlvl; 40457416Smarkm dv.dv_maxlvl = ss.ss_maxlvl; 40557416Smarkm dv.dv_valcmp = ss.ss_maxcmp; 40657416Smarkm } 40757416Smarkm#endif /* SO_SEC_MULTI */ 40857416Smarkm } 40957416Smarkm#endif /* _SC_CRAY_SECURE_SYS */ 41057416Smarkm 41157416Smarkm roken_openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON); 41257416Smarkm sa_size = sizeof (__ss); 41357416Smarkm if (getpeername(STDIN_FILENO, sa, &sa_size) < 0) { 41457416Smarkm fprintf(stderr, "%s: ", progname); 41557416Smarkm perror("getpeername"); 41657416Smarkm _exit(1); 41757416Smarkm } 41857416Smarkm if (keepalive && 41957416Smarkm setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE, 42057416Smarkm (void *)&on, sizeof (on)) < 0) { 42157416Smarkm syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 42257416Smarkm } 42357416Smarkm 42457416Smarkm#if defined(IPPROTO_IP) && defined(IP_TOS) && defined(HAVE_SETSOCKOPT) 42557416Smarkm { 42657416Smarkm# ifdef HAVE_GETTOSBYNAME 42757416Smarkm struct tosent *tp; 42857416Smarkm if (tos < 0 && (tp = gettosbyname("telnet", "tcp"))) 42957416Smarkm tos = tp->t_tos; 43057416Smarkm# endif 43157416Smarkm if (tos < 0) 43257416Smarkm tos = 020; /* Low Delay bit */ 43357416Smarkm if (tos 43457416Smarkm && sa->sa_family == AF_INET 43557416Smarkm && (setsockopt(STDIN_FILENO, IPPROTO_IP, IP_TOS, 43657416Smarkm (void *)&tos, sizeof(tos)) < 0) 43757416Smarkm && (errno != ENOPROTOOPT) ) 43857416Smarkm syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); 43957416Smarkm } 44057416Smarkm#endif /* defined(IPPROTO_IP) && defined(IP_TOS) */ 44157416Smarkm net = STDIN_FILENO; 44257416Smarkm doit(sa, sa_size); 44357416Smarkm /* NOTREACHED */ 44457416Smarkm return 0; 44557416Smarkm} /* end of main */ 44657416Smarkm 44757416Smarkmstatic void 44857416Smarkmusage(void) 44957416Smarkm{ 45057416Smarkm fprintf(stderr, "Usage: telnetd"); 45157416Smarkm#ifdef AUTHENTICATION 45257416Smarkm fprintf(stderr, " [-a (debug|other|otp|user|valid|off|none)]\n\t"); 45357416Smarkm#endif 45457416Smarkm fprintf(stderr, " [-debug]"); 45557416Smarkm#ifdef DIAGNOSTICS 45657416Smarkm fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t"); 45757416Smarkm#endif 45857416Smarkm#ifdef AUTHENTICATION 45957416Smarkm fprintf(stderr, " [-edebug]"); 46057416Smarkm#endif 46157416Smarkm fprintf(stderr, " [-h]"); 46257416Smarkm fprintf(stderr, " [-L login]"); 46357416Smarkm fprintf(stderr, " [-n]"); 46457416Smarkm#ifdef _CRAY 46557416Smarkm fprintf(stderr, " [-r[lowpty]-[highpty]]"); 46657416Smarkm#endif 46757416Smarkm fprintf(stderr, "\n\t"); 46857416Smarkm#ifdef HAVE_GETTOSBYNAME 46957416Smarkm fprintf(stderr, " [-S tos]"); 47057416Smarkm#endif 47157416Smarkm#ifdef AUTHENTICATION 47257416Smarkm fprintf(stderr, " [-X auth-type] [-y] [-z]"); 47357416Smarkm#endif 47457416Smarkm fprintf(stderr, " [-u utmp_hostname_length] [-U]"); 47557416Smarkm fprintf(stderr, " [port]\n"); 47657416Smarkm exit(1); 47757416Smarkm} 47857416Smarkm 47957416Smarkm/* 48057416Smarkm * getterminaltype 48157416Smarkm * 48257416Smarkm * Ask the other end to send along its terminal type and speed. 48357416Smarkm * Output is the variable terminaltype filled in. 48457416Smarkm */ 48557416Smarkmstatic unsigned char ttytype_sbbuf[] = { 48657416Smarkm IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE 48757416Smarkm}; 48857416Smarkm 48957416Smarkmint 49057416Smarkmgetterminaltype(char *name, size_t name_sz) 49157416Smarkm{ 49257416Smarkm int retval = -1; 49357416Smarkm void _gettermname(); 49457416Smarkm 49557416Smarkm settimer(baseline); 49657416Smarkm#ifdef AUTHENTICATION 49757416Smarkm /* 49857416Smarkm * Handle the Authentication option before we do anything else. 49957416Smarkm */ 50057416Smarkm send_do(TELOPT_AUTHENTICATION, 1); 50157416Smarkm while (his_will_wont_is_changing(TELOPT_AUTHENTICATION)) 50257416Smarkm ttloop(); 50357416Smarkm if (his_state_is_will(TELOPT_AUTHENTICATION)) { 50457416Smarkm retval = auth_wait(name, name_sz); 50557416Smarkm } 50657416Smarkm#endif 50757416Smarkm 50857416Smarkm#ifdef ENCRYPTION 50957416Smarkm send_will(TELOPT_ENCRYPT, 1); 51057416Smarkm send_do(TELOPT_ENCRYPT, 1); /* esc@magic.fi */ 51157416Smarkm#endif 51257416Smarkm send_do(TELOPT_TTYPE, 1); 51357416Smarkm send_do(TELOPT_TSPEED, 1); 51457416Smarkm send_do(TELOPT_XDISPLOC, 1); 51557416Smarkm send_do(TELOPT_NEW_ENVIRON, 1); 51657416Smarkm send_do(TELOPT_OLD_ENVIRON, 1); 51757416Smarkm while ( 51857416Smarkm#ifdef ENCRYPTION 51957416Smarkm his_do_dont_is_changing(TELOPT_ENCRYPT) || 52057416Smarkm#endif 52157416Smarkm his_will_wont_is_changing(TELOPT_TTYPE) || 52257416Smarkm his_will_wont_is_changing(TELOPT_TSPEED) || 52357416Smarkm his_will_wont_is_changing(TELOPT_XDISPLOC) || 52457416Smarkm his_will_wont_is_changing(TELOPT_NEW_ENVIRON) || 52557416Smarkm his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) { 52657416Smarkm ttloop(); 52757416Smarkm } 52857416Smarkm#ifdef ENCRYPTION 52957416Smarkm /* 53057416Smarkm * Wait for the negotiation of what type of encryption we can 53157416Smarkm * send with. If autoencrypt is not set, this will just return. 53257416Smarkm */ 53357416Smarkm if (his_state_is_will(TELOPT_ENCRYPT)) { 53457416Smarkm encrypt_wait(); 53557416Smarkm } 53657416Smarkm#endif 53757416Smarkm if (his_state_is_will(TELOPT_TSPEED)) { 53857416Smarkm static unsigned char sb[] = 53957416Smarkm { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE }; 54057416Smarkm 54157416Smarkm telnet_net_write (sb, sizeof sb); 54257416Smarkm DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2);); 54357416Smarkm } 54457416Smarkm if (his_state_is_will(TELOPT_XDISPLOC)) { 54557416Smarkm static unsigned char sb[] = 54657416Smarkm { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE }; 54757416Smarkm 54857416Smarkm telnet_net_write (sb, sizeof sb); 54957416Smarkm DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2);); 55057416Smarkm } 55157416Smarkm if (his_state_is_will(TELOPT_NEW_ENVIRON)) { 55257416Smarkm static unsigned char sb[] = 55357416Smarkm { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE }; 55457416Smarkm 55557416Smarkm telnet_net_write (sb, sizeof sb); 55657416Smarkm DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2);); 55757416Smarkm } 55857416Smarkm else if (his_state_is_will(TELOPT_OLD_ENVIRON)) { 55957416Smarkm static unsigned char sb[] = 56057416Smarkm { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE }; 56157416Smarkm 56257416Smarkm telnet_net_write (sb, sizeof sb); 56357416Smarkm DIAG(TD_OPTIONS, printsub('>', sb + 2, sizeof sb - 2);); 56457416Smarkm } 56557416Smarkm if (his_state_is_will(TELOPT_TTYPE)) { 56657416Smarkm 56757416Smarkm telnet_net_write (ttytype_sbbuf, sizeof ttytype_sbbuf); 56857416Smarkm DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2, 56957416Smarkm sizeof ttytype_sbbuf - 2);); 57057416Smarkm } 57157416Smarkm if (his_state_is_will(TELOPT_TSPEED)) { 57257416Smarkm while (sequenceIs(tspeedsubopt, baseline)) 57357416Smarkm ttloop(); 57457416Smarkm } 57557416Smarkm if (his_state_is_will(TELOPT_XDISPLOC)) { 57657416Smarkm while (sequenceIs(xdisplocsubopt, baseline)) 57757416Smarkm ttloop(); 57857416Smarkm } 57957416Smarkm if (his_state_is_will(TELOPT_NEW_ENVIRON)) { 58057416Smarkm while (sequenceIs(environsubopt, baseline)) 58157416Smarkm ttloop(); 58257416Smarkm } 58357416Smarkm if (his_state_is_will(TELOPT_OLD_ENVIRON)) { 58457416Smarkm while (sequenceIs(oenvironsubopt, baseline)) 58557416Smarkm ttloop(); 58657416Smarkm } 58757416Smarkm if (his_state_is_will(TELOPT_TTYPE)) { 58857416Smarkm char first[256], last[256]; 58957416Smarkm 59057416Smarkm while (sequenceIs(ttypesubopt, baseline)) 59157416Smarkm ttloop(); 59257416Smarkm 59357416Smarkm /* 59457416Smarkm * If the other side has already disabled the option, then 59557416Smarkm * we have to just go with what we (might) have already gotten. 59657416Smarkm */ 59757416Smarkm if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) { 59857416Smarkm strlcpy(first, terminaltype, sizeof(first)); 59957416Smarkm for(;;) { 60057416Smarkm /* 60157416Smarkm * Save the unknown name, and request the next name. 60257416Smarkm */ 60357416Smarkm strlcpy(last, terminaltype, sizeof(last)); 60457416Smarkm _gettermname(); 60557416Smarkm if (terminaltypeok(terminaltype)) 60657416Smarkm break; 60757416Smarkm if ((strncmp(last, terminaltype, sizeof(last)) == 0) || 60857416Smarkm his_state_is_wont(TELOPT_TTYPE)) { 60957416Smarkm /* 61057416Smarkm * We've hit the end. If this is the same as 61157416Smarkm * the first name, just go with it. 61257416Smarkm */ 61357416Smarkm if (strncmp(first, terminaltype, sizeof(first)) == 0) 61457416Smarkm break; 61557416Smarkm /* 61657416Smarkm * Get the terminal name one more time, so that 61757416Smarkm * RFC1091 compliant telnets will cycle back to 61857416Smarkm * the start of the list. 61957416Smarkm */ 62057416Smarkm _gettermname(); 62157416Smarkm if (strncmp(first, terminaltype, sizeof(first)) != 0) 62257416Smarkm strcpy(terminaltype, first); 62357416Smarkm break; 62457416Smarkm } 62557416Smarkm } 62657416Smarkm } 62757416Smarkm } 62857416Smarkm return(retval); 62957416Smarkm} /* end of getterminaltype */ 63057416Smarkm 63157416Smarkmvoid 63257416Smarkm_gettermname() 63357416Smarkm{ 63457416Smarkm /* 63557416Smarkm * If the client turned off the option, 63657416Smarkm * we can't send another request, so we 63757416Smarkm * just return. 63857416Smarkm */ 63957416Smarkm if (his_state_is_wont(TELOPT_TTYPE)) 64057416Smarkm return; 64157416Smarkm settimer(baseline); 64257416Smarkm telnet_net_write (ttytype_sbbuf, sizeof ttytype_sbbuf); 64357416Smarkm DIAG(TD_OPTIONS, printsub('>', ttytype_sbbuf + 2, 64457416Smarkm sizeof ttytype_sbbuf - 2);); 64557416Smarkm while (sequenceIs(ttypesubopt, baseline)) 64657416Smarkm ttloop(); 64757416Smarkm} 64857416Smarkm 64957416Smarkmint 65057416Smarkmterminaltypeok(char *s) 65157416Smarkm{ 65257416Smarkm return 1; 65357416Smarkm} 65457416Smarkm 65557416Smarkm 65657416Smarkmchar *hostname; 65757416Smarkmchar host_name[MaxHostNameLen]; 65857416Smarkmchar remote_host_name[MaxHostNameLen]; 65957416Smarkm 66057416Smarkm/* 66157416Smarkm * Get a pty, scan input lines. 66257416Smarkm */ 66357416Smarkmstatic void 66457416Smarkmdoit(struct sockaddr *who, int who_len) 66557416Smarkm{ 66657416Smarkm char *host = NULL; 66757416Smarkm int level; 66857416Smarkm int ptynum; 66957416Smarkm char user_name[256]; 67057416Smarkm int error; 67157416Smarkm char host_addr[256]; 67257416Smarkm 67357416Smarkm /* 67457416Smarkm * Find an available pty to use. 67557416Smarkm */ 67657416Smarkm ourpty = getpty(&ptynum); 67757416Smarkm if (ourpty < 0) 67857416Smarkm fatal(net, "All network ports in use"); 67957416Smarkm 68057416Smarkm#ifdef _SC_CRAY_SECURE_SYS 68157416Smarkm /* 68257416Smarkm * set ttyp line security label 68357416Smarkm */ 68457416Smarkm if (secflag) { 68557416Smarkm char slave_dev[16]; 68657416Smarkm 68757416Smarkm snprintf(tty_dev, sizeof(tty_dev), "/dev/pty/%03d", ptynum); 68857416Smarkm if (setdevs(tty_dev, &dv) < 0) 68957416Smarkm fatal(net, "cannot set pty security"); 69057416Smarkm snprintf(slave_dev, sizeof(slave_dev), "/dev/ttyp%03d", ptynum); 69157416Smarkm if (setdevs(slave_dev, &dv) < 0) 69257416Smarkm fatal(net, "cannot set tty security"); 69357416Smarkm } 69457416Smarkm#endif /* _SC_CRAY_SECURE_SYS */ 69557416Smarkm 69657416Smarkm error = getnameinfo_verified (who, who_len, host_addr, sizeof(host_addr), 69757416Smarkm NULL, 0, 69857416Smarkm registerd_host_only ? NI_NAMEREQD : 0); 69957416Smarkm if (error) 70057416Smarkm fatal(net, "Couldn't resolve your address into a host name.\r\n\ 70157416SmarkmPlease contact your net administrator"); 70257416Smarkm 70357416Smarkm /* 70457416Smarkm * We must make a copy because Kerberos is probably going 70557416Smarkm * to also do a gethost* and overwrite the static data... 70657416Smarkm */ 70757416Smarkm strlcpy(remote_host_name, host_addr, sizeof(remote_host_name)); 70857416Smarkm host = remote_host_name; 70957416Smarkm 71057416Smarkm /* XXX - should be k_gethostname? */ 71157416Smarkm gethostname(host_name, sizeof (host_name)); 71257416Smarkm hostname = host_name; 71357416Smarkm 71457416Smarkm /* Only trim if too long (and possible) */ 71557416Smarkm if (strlen(remote_host_name) > abs(utmp_len)) { 71657416Smarkm char *domain = strchr(host_name, '.'); 71757416Smarkm char *p = strchr(remote_host_name, '.'); 71857416Smarkm if (domain && p && (strcmp(p, domain) == 0)) 71957416Smarkm *p = 0; /* remove domain part */ 72057416Smarkm } 72157416Smarkm 72257416Smarkm 72357416Smarkm /* 72457416Smarkm * If hostname still doesn't fit utmp, use ipaddr. 72557416Smarkm */ 72657416Smarkm if (strlen(remote_host_name) > abs(utmp_len)) 72757416Smarkm strlcpy(remote_host_name, 72857416Smarkm host_addr, 72957416Smarkm sizeof(remote_host_name)); 73057416Smarkm 73157416Smarkm#ifdef AUTHENTICATION 73257416Smarkm auth_encrypt_init(hostname, host, "TELNETD", 1); 73357416Smarkm#endif 73457416Smarkm 73557416Smarkm init_env(); 73657416Smarkm /* 73757416Smarkm * get terminal type. 73857416Smarkm */ 73957416Smarkm *user_name = 0; 74057416Smarkm level = getterminaltype(user_name, sizeof(user_name)); 74172445Sassar esetenv("TERM", terminaltype ? terminaltype : "network", 1); 74257416Smarkm 74357416Smarkm#ifdef _SC_CRAY_SECURE_SYS 74457416Smarkm if (secflag) { 74557416Smarkm if (setulvl(dv.dv_actlvl) < 0) 74657416Smarkm fatal(net,"cannot setulvl()"); 74757416Smarkm if (setucmp(dv.dv_actcmp) < 0) 74857416Smarkm fatal(net, "cannot setucmp()"); 74957416Smarkm } 75057416Smarkm#endif /* _SC_CRAY_SECURE_SYS */ 75157416Smarkm 75257416Smarkm /* begin server processing */ 75357416Smarkm my_telnet(net, ourpty, host, level, user_name); 75457416Smarkm /*NOTREACHED*/ 75557416Smarkm} /* end of doit */ 75657416Smarkm 75757416Smarkm/* output contents of /etc/issue.net, or /etc/issue */ 75857416Smarkmstatic void 75957416Smarkmshow_issue(void) 76057416Smarkm{ 76157416Smarkm FILE *f; 76257416Smarkm char buf[128]; 76357416Smarkm f = fopen("/etc/issue.net", "r"); 76457416Smarkm if(f == NULL) 76557416Smarkm f = fopen("/etc/issue", "r"); 76657416Smarkm if(f){ 76757416Smarkm while(fgets(buf, sizeof(buf)-2, f)){ 76857416Smarkm strcpy(buf + strcspn(buf, "\r\n"), "\r\n"); 76957416Smarkm writenet((unsigned char*)buf, strlen(buf)); 77057416Smarkm } 77157416Smarkm fclose(f); 77257416Smarkm } 77357416Smarkm} 77457416Smarkm 77557416Smarkm/* 77657416Smarkm * Main loop. Select from pty and network, and 77757416Smarkm * hand data to telnet receiver finite state machine. 77857416Smarkm */ 77957416Smarkmvoid 78057416Smarkmmy_telnet(int f, int p, char *host, int level, char *autoname) 78157416Smarkm{ 78257416Smarkm int on = 1; 78357416Smarkm char *he; 78457416Smarkm char *IM; 78557416Smarkm int nfd; 78657416Smarkm int startslave_called = 0; 78757416Smarkm time_t timeout; 78857416Smarkm 78957416Smarkm /* 79057416Smarkm * Initialize the slc mapping table. 79157416Smarkm */ 79257416Smarkm get_slc_defaults(); 79357416Smarkm 79457416Smarkm /* 79557416Smarkm * Do some tests where it is desireable to wait for a response. 79657416Smarkm * Rather than doing them slowly, one at a time, do them all 79757416Smarkm * at once. 79857416Smarkm */ 79957416Smarkm if (my_state_is_wont(TELOPT_SGA)) 80057416Smarkm send_will(TELOPT_SGA, 1); 80157416Smarkm /* 80257416Smarkm * Is the client side a 4.2 (NOT 4.3) system? We need to know this 80357416Smarkm * because 4.2 clients are unable to deal with TCP urgent data. 80457416Smarkm * 80557416Smarkm * To find out, we send out a "DO ECHO". If the remote system 80657416Smarkm * answers "WILL ECHO" it is probably a 4.2 client, and we note 80757416Smarkm * that fact ("WILL ECHO" ==> that the client will echo what 80857416Smarkm * WE, the server, sends it; it does NOT mean that the client will 80957416Smarkm * echo the terminal input). 81057416Smarkm */ 81157416Smarkm send_do(TELOPT_ECHO, 1); 81257416Smarkm 81357416Smarkm /* 81457416Smarkm * Send along a couple of other options that we wish to negotiate. 81557416Smarkm */ 81657416Smarkm send_do(TELOPT_NAWS, 1); 81757416Smarkm send_will(TELOPT_STATUS, 1); 81857416Smarkm flowmode = 1; /* default flow control state */ 81957416Smarkm restartany = -1; /* uninitialized... */ 82057416Smarkm send_do(TELOPT_LFLOW, 1); 82157416Smarkm 82257416Smarkm /* 82357416Smarkm * Spin, waiting for a response from the DO ECHO. However, 82457416Smarkm * some REALLY DUMB telnets out there might not respond 82557416Smarkm * to the DO ECHO. So, we spin looking for NAWS, (most dumb 82657416Smarkm * telnets so far seem to respond with WONT for a DO that 82757416Smarkm * they don't understand...) because by the time we get the 82857416Smarkm * response, it will already have processed the DO ECHO. 82957416Smarkm * Kludge upon kludge. 83057416Smarkm */ 83157416Smarkm while (his_will_wont_is_changing(TELOPT_NAWS)) 83257416Smarkm ttloop(); 83357416Smarkm 83457416Smarkm /* 83557416Smarkm * But... 83657416Smarkm * The client might have sent a WILL NAWS as part of its 83757416Smarkm * startup code; if so, we'll be here before we get the 83857416Smarkm * response to the DO ECHO. We'll make the assumption 83957416Smarkm * that any implementation that understands about NAWS 84057416Smarkm * is a modern enough implementation that it will respond 84157416Smarkm * to our DO ECHO request; hence we'll do another spin 84257416Smarkm * waiting for the ECHO option to settle down, which is 84357416Smarkm * what we wanted to do in the first place... 84457416Smarkm */ 84557416Smarkm if (his_want_state_is_will(TELOPT_ECHO) && 84657416Smarkm his_state_is_will(TELOPT_NAWS)) { 84757416Smarkm while (his_will_wont_is_changing(TELOPT_ECHO)) 84857416Smarkm ttloop(); 84957416Smarkm } 85057416Smarkm /* 85157416Smarkm * On the off chance that the telnet client is broken and does not 85257416Smarkm * respond to the DO ECHO we sent, (after all, we did send the 85357416Smarkm * DO NAWS negotiation after the DO ECHO, and we won't get here 85457416Smarkm * until a response to the DO NAWS comes back) simulate the 85557416Smarkm * receipt of a will echo. This will also send a WONT ECHO 85657416Smarkm * to the client, since we assume that the client failed to 85757416Smarkm * respond because it believes that it is already in DO ECHO 85857416Smarkm * mode, which we do not want. 85957416Smarkm */ 86057416Smarkm if (his_want_state_is_will(TELOPT_ECHO)) { 86157416Smarkm DIAG(TD_OPTIONS, 86257416Smarkm {output_data("td: simulating recv\r\n"); 86357416Smarkm }); 86457416Smarkm willoption(TELOPT_ECHO); 86557416Smarkm } 86657416Smarkm 86757416Smarkm /* 86857416Smarkm * Finally, to clean things up, we turn on our echo. This 86957416Smarkm * will break stupid 4.2 telnets out of local terminal echo. 87057416Smarkm */ 87157416Smarkm 87257416Smarkm if (my_state_is_wont(TELOPT_ECHO)) 87357416Smarkm send_will(TELOPT_ECHO, 1); 87457416Smarkm 87557416Smarkm#ifdef TIOCPKT 87657416Smarkm#ifdef STREAMSPTY 87757416Smarkm if (!really_stream) 87857416Smarkm#endif 87957416Smarkm /* 88057416Smarkm * Turn on packet mode 88157416Smarkm */ 88257416Smarkm ioctl(p, TIOCPKT, (char *)&on); 88357416Smarkm#endif 88457416Smarkm 88557416Smarkm 88657416Smarkm /* 88757416Smarkm * Call telrcv() once to pick up anything received during 88857416Smarkm * terminal type negotiation, 4.2/4.3 determination, and 88957416Smarkm * linemode negotiation. 89057416Smarkm */ 89157416Smarkm telrcv(); 89257416Smarkm 89357416Smarkm ioctl(f, FIONBIO, (char *)&on); 89457416Smarkm ioctl(p, FIONBIO, (char *)&on); 89557416Smarkm 89657416Smarkm#if defined(SO_OOBINLINE) && defined(HAVE_SETSOCKOPT) 89757416Smarkm setsockopt(net, SOL_SOCKET, SO_OOBINLINE, 89857416Smarkm (void *)&on, sizeof on); 89957416Smarkm#endif /* defined(SO_OOBINLINE) */ 90057416Smarkm 90157416Smarkm#ifdef SIGTSTP 90257416Smarkm signal(SIGTSTP, SIG_IGN); 90357416Smarkm#endif 90457416Smarkm#ifdef SIGTTOU 90557416Smarkm /* 90657416Smarkm * Ignoring SIGTTOU keeps the kernel from blocking us 90757416Smarkm * in ttioct() in /sys/tty.c. 90857416Smarkm */ 90957416Smarkm signal(SIGTTOU, SIG_IGN); 91057416Smarkm#endif 91157416Smarkm 91257416Smarkm signal(SIGCHLD, cleanup); 91357416Smarkm 91457416Smarkm#ifdef TIOCNOTTY 91557416Smarkm { 91657416Smarkm int t; 91757416Smarkm t = open(_PATH_TTY, O_RDWR); 91857416Smarkm if (t >= 0) { 91957416Smarkm ioctl(t, TIOCNOTTY, (char *)0); 92057416Smarkm close(t); 92157416Smarkm } 92257416Smarkm } 92357416Smarkm#endif 92457416Smarkm 92557416Smarkm show_issue(); 92657416Smarkm /* 92757416Smarkm * Show banner that getty never gave. 92857416Smarkm * 92957416Smarkm * We put the banner in the pty input buffer. This way, it 93057416Smarkm * gets carriage return null processing, etc., just like all 93157416Smarkm * other pty --> client data. 93257416Smarkm */ 93357416Smarkm 93457416Smarkm if (getenv("USER")) 93557416Smarkm hostinfo = 0; 93657416Smarkm 93757416Smarkm IM = DEFAULT_IM; 93857416Smarkm he = 0; 93957416Smarkm edithost(he, host_name); 94057416Smarkm if (hostinfo && *IM) 94157416Smarkm putf(IM, ptyibuf2); 94257416Smarkm 94357416Smarkm if (pcc) 94457416Smarkm strncat(ptyibuf2, ptyip, pcc+1); 94557416Smarkm ptyip = ptyibuf2; 94657416Smarkm pcc = strlen(ptyip); 94757416Smarkm 94857416Smarkm DIAG(TD_REPORT, { 94957416Smarkm output_data("td: Entering processing loop\r\n"); 95057416Smarkm }); 95157416Smarkm 95257416Smarkm 95357416Smarkm nfd = ((f > p) ? f : p) + 1; 95457416Smarkm timeout = time(NULL) + 5; 95557416Smarkm for (;;) { 95657416Smarkm fd_set ibits, obits, xbits; 95757416Smarkm int c; 95857416Smarkm 95957416Smarkm /* wait for encryption to be turned on, but don't wait 96057416Smarkm indefinitely */ 96157416Smarkm if(!startslave_called && (!encrypt_delay() || timeout > time(NULL))){ 96257416Smarkm startslave_called = 1; 96357416Smarkm startslave(host, level, autoname); 96457416Smarkm } 96557416Smarkm 96657416Smarkm if (ncc < 0 && pcc < 0) 96757416Smarkm break; 96857416Smarkm 96957416Smarkm FD_ZERO(&ibits); 97057416Smarkm FD_ZERO(&obits); 97157416Smarkm FD_ZERO(&xbits); 97272445Sassar 97372445Sassar if (f >= FD_SETSIZE 97472445Sassar || p >= FD_SETSIZE) 97572445Sassar fatal(net, "fd too large"); 97672445Sassar 97757416Smarkm /* 97857416Smarkm * Never look for input if there's still 97957416Smarkm * stuff in the corresponding output buffer 98057416Smarkm */ 98157416Smarkm if (nfrontp - nbackp || pcc > 0) { 98257416Smarkm FD_SET(f, &obits); 98357416Smarkm } else { 98457416Smarkm FD_SET(p, &ibits); 98557416Smarkm } 98657416Smarkm if (pfrontp - pbackp || ncc > 0) { 98757416Smarkm FD_SET(p, &obits); 98857416Smarkm } else { 98957416Smarkm FD_SET(f, &ibits); 99057416Smarkm } 99157416Smarkm if (!SYNCHing) { 99257416Smarkm FD_SET(f, &xbits); 99357416Smarkm } 99457416Smarkm if ((c = select(nfd, &ibits, &obits, &xbits, 99557416Smarkm (struct timeval *)0)) < 1) { 99657416Smarkm if (c == -1) { 99757416Smarkm if (errno == EINTR) { 99857416Smarkm continue; 99957416Smarkm } 100057416Smarkm } 100157416Smarkm sleep(5); 100257416Smarkm continue; 100357416Smarkm } 100457416Smarkm 100557416Smarkm /* 100657416Smarkm * Any urgent data? 100757416Smarkm */ 100857416Smarkm if (FD_ISSET(net, &xbits)) { 100957416Smarkm SYNCHing = 1; 101057416Smarkm } 101157416Smarkm 101257416Smarkm /* 101357416Smarkm * Something to read from the network... 101457416Smarkm */ 101557416Smarkm if (FD_ISSET(net, &ibits)) { 101657416Smarkm#ifndef SO_OOBINLINE 101757416Smarkm /* 101857416Smarkm * In 4.2 (and 4.3 beta) systems, the 101957416Smarkm * OOB indication and data handling in the kernel 102057416Smarkm * is such that if two separate TCP Urgent requests 102157416Smarkm * come in, one byte of TCP data will be overlaid. 102257416Smarkm * This is fatal for Telnet, but we try to live 102357416Smarkm * with it. 102457416Smarkm * 102557416Smarkm * In addition, in 4.2 (and...), a special protocol 102657416Smarkm * is needed to pick up the TCP Urgent data in 102757416Smarkm * the correct sequence. 102857416Smarkm * 102957416Smarkm * What we do is: if we think we are in urgent 103057416Smarkm * mode, we look to see if we are "at the mark". 103157416Smarkm * If we are, we do an OOB receive. If we run 103257416Smarkm * this twice, we will do the OOB receive twice, 103357416Smarkm * but the second will fail, since the second 103457416Smarkm * time we were "at the mark", but there wasn't 103557416Smarkm * any data there (the kernel doesn't reset 103657416Smarkm * "at the mark" until we do a normal read). 103757416Smarkm * Once we've read the OOB data, we go ahead 103857416Smarkm * and do normal reads. 103957416Smarkm * 104057416Smarkm * There is also another problem, which is that 104157416Smarkm * since the OOB byte we read doesn't put us 104257416Smarkm * out of OOB state, and since that byte is most 104357416Smarkm * likely the TELNET DM (data mark), we would 104457416Smarkm * stay in the TELNET SYNCH (SYNCHing) state. 104557416Smarkm * So, clocks to the rescue. If we've "just" 104657416Smarkm * received a DM, then we test for the 104757416Smarkm * presence of OOB data when the receive OOB 104857416Smarkm * fails (and AFTER we did the normal mode read 104957416Smarkm * to clear "at the mark"). 105057416Smarkm */ 105157416Smarkm if (SYNCHing) { 105257416Smarkm int atmark; 105357416Smarkm 105457416Smarkm ioctl(net, SIOCATMARK, (char *)&atmark); 105557416Smarkm if (atmark) { 105657416Smarkm ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB); 105757416Smarkm if ((ncc == -1) && (errno == EINVAL)) { 105857416Smarkm ncc = read(net, netibuf, sizeof (netibuf)); 105957416Smarkm if (sequenceIs(didnetreceive, gotDM)) { 106057416Smarkm SYNCHing = stilloob(net); 106157416Smarkm } 106257416Smarkm } 106357416Smarkm } else { 106457416Smarkm ncc = read(net, netibuf, sizeof (netibuf)); 106557416Smarkm } 106657416Smarkm } else { 106757416Smarkm ncc = read(net, netibuf, sizeof (netibuf)); 106857416Smarkm } 106957416Smarkm settimer(didnetreceive); 107057416Smarkm#else /* !defined(SO_OOBINLINE)) */ 107157416Smarkm ncc = read(net, netibuf, sizeof (netibuf)); 107257416Smarkm#endif /* !defined(SO_OOBINLINE)) */ 107357416Smarkm if (ncc < 0 && errno == EWOULDBLOCK) 107457416Smarkm ncc = 0; 107557416Smarkm else { 107657416Smarkm if (ncc <= 0) { 107757416Smarkm break; 107857416Smarkm } 107957416Smarkm netip = netibuf; 108057416Smarkm } 108157416Smarkm DIAG((TD_REPORT | TD_NETDATA), { 108257416Smarkm output_data("td: netread %d chars\r\n", ncc); 108357416Smarkm }); 108457416Smarkm DIAG(TD_NETDATA, printdata("nd", netip, ncc)); 108557416Smarkm } 108657416Smarkm 108757416Smarkm /* 108857416Smarkm * Something to read from the pty... 108957416Smarkm */ 109057416Smarkm if (FD_ISSET(p, &ibits)) { 109157416Smarkm#ifdef STREAMSPTY 109257416Smarkm if (really_stream) 109357416Smarkm pcc = readstream(p, ptyibuf, BUFSIZ); 109457416Smarkm else 109557416Smarkm#endif 109657416Smarkm pcc = read(p, ptyibuf, BUFSIZ); 109757416Smarkm 109857416Smarkm /* 109957416Smarkm * On some systems, if we try to read something 110057416Smarkm * off the master side before the slave side is 110157416Smarkm * opened, we get EIO. 110257416Smarkm */ 110357416Smarkm if (pcc < 0 && (errno == EWOULDBLOCK || 110457416Smarkm#ifdef EAGAIN 110557416Smarkm errno == EAGAIN || 110657416Smarkm#endif 110757416Smarkm errno == EIO)) { 110857416Smarkm pcc = 0; 110957416Smarkm } else { 111057416Smarkm if (pcc <= 0) 111157416Smarkm break; 111257416Smarkm if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) { 111357416Smarkm netclear(); /* clear buffer back */ 111457416Smarkm#ifndef NO_URGENT 111557416Smarkm /* 111657416Smarkm * There are client telnets on some 111757416Smarkm * operating systems get screwed up 111857416Smarkm * royally if we send them urgent 111957416Smarkm * mode data. 112057416Smarkm */ 112157416Smarkm output_data ("%c%c", IAC, DM); 112257416Smarkm 112357416Smarkm neturg = nfrontp-1; /* off by one XXX */ 112457416Smarkm DIAG(TD_OPTIONS, 112557416Smarkm printoption("td: send IAC", DM)); 112657416Smarkm 112757416Smarkm#endif 112857416Smarkm } 112957416Smarkm if (his_state_is_will(TELOPT_LFLOW) && 113057416Smarkm (ptyibuf[0] & 113157416Smarkm (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) { 113257416Smarkm int newflow = 113357416Smarkm ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0; 113457416Smarkm if (newflow != flowmode) { 113557416Smarkm flowmode = newflow; 113657416Smarkm output_data("%c%c%c%c%c%c", 113757416Smarkm IAC, SB, TELOPT_LFLOW, 113857416Smarkm flowmode ? LFLOW_ON 113957416Smarkm : LFLOW_OFF, 114057416Smarkm IAC, SE); 114157416Smarkm DIAG(TD_OPTIONS, printsub('>', 114257416Smarkm (unsigned char *)nfrontp-4, 114357416Smarkm 4);); 114457416Smarkm } 114557416Smarkm } 114657416Smarkm pcc--; 114757416Smarkm ptyip = ptyibuf+1; 114857416Smarkm } 114957416Smarkm } 115057416Smarkm 115157416Smarkm while (pcc > 0) { 115257416Smarkm if ((&netobuf[BUFSIZ] - nfrontp) < 3) 115357416Smarkm break; 115457416Smarkm c = *ptyip++ & 0377, pcc--; 115557416Smarkm if (c == IAC) 115657416Smarkm *nfrontp++ = c; 115757416Smarkm *nfrontp++ = c; 115857416Smarkm if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) { 115957416Smarkm if (pcc > 0 && ((*ptyip & 0377) == '\n')) { 116057416Smarkm *nfrontp++ = *ptyip++ & 0377; 116157416Smarkm pcc--; 116257416Smarkm } else 116357416Smarkm *nfrontp++ = '\0'; 116457416Smarkm } 116557416Smarkm } 116657416Smarkm 116757416Smarkm if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0) 116857416Smarkm netflush(); 116957416Smarkm if (ncc > 0) 117057416Smarkm telrcv(); 117157416Smarkm if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0) 117257416Smarkm ptyflush(); 117357416Smarkm } 117457416Smarkm cleanup(0); 117557416Smarkm} 117657416Smarkm 117757416Smarkm#ifndef TCSIG 117857416Smarkm# ifdef TIOCSIG 117957416Smarkm# define TCSIG TIOCSIG 118057416Smarkm# endif 118157416Smarkm#endif 118257416Smarkm 118357416Smarkm#ifdef STREAMSPTY 118457416Smarkm 118557416Smarkm int flowison = -1; /* current state of flow: -1 is unknown */ 118657416Smarkm 118757416Smarkmint 118857416Smarkmreadstream(int p, char *ibuf, int bufsize) 118957416Smarkm{ 119057416Smarkm int flags = 0; 119157416Smarkm int ret = 0; 119257416Smarkm struct termios *tsp; 119357416Smarkm#if 0 119457416Smarkm struct termio *tp; 119557416Smarkm#endif 119657416Smarkm struct iocblk *ip; 119757416Smarkm char vstop, vstart; 119857416Smarkm int ixon; 119957416Smarkm int newflow; 120057416Smarkm 120157416Smarkm strbufc.maxlen = BUFSIZ; 120257416Smarkm strbufc.buf = (char *)ctlbuf; 120357416Smarkm strbufd.maxlen = bufsize-1; 120457416Smarkm strbufd.len = 0; 120557416Smarkm strbufd.buf = ibuf+1; 120657416Smarkm ibuf[0] = 0; 120757416Smarkm 120857416Smarkm ret = getmsg(p, &strbufc, &strbufd, &flags); 120957416Smarkm if (ret < 0) /* error of some sort -- probably EAGAIN */ 121057416Smarkm return(-1); 121157416Smarkm 121257416Smarkm if (strbufc.len <= 0 || ctlbuf[0] == M_DATA) { 121357416Smarkm /* data message */ 121457416Smarkm if (strbufd.len > 0) { /* real data */ 121557416Smarkm return(strbufd.len + 1); /* count header char */ 121657416Smarkm } else { 121757416Smarkm /* nothing there */ 121857416Smarkm errno = EAGAIN; 121957416Smarkm return(-1); 122057416Smarkm } 122157416Smarkm } 122257416Smarkm 122357416Smarkm /* 122457416Smarkm * It's a control message. Return 1, to look at the flag we set 122557416Smarkm */ 122657416Smarkm 122757416Smarkm switch (ctlbuf[0]) { 122857416Smarkm case M_FLUSH: 122957416Smarkm if (ibuf[1] & FLUSHW) 123057416Smarkm ibuf[0] = TIOCPKT_FLUSHWRITE; 123157416Smarkm return(1); 123257416Smarkm 123357416Smarkm case M_IOCTL: 123457416Smarkm ip = (struct iocblk *) (ibuf+1); 123557416Smarkm 123657416Smarkm switch (ip->ioc_cmd) { 123757416Smarkm#ifdef TCSETS 123857416Smarkm case TCSETS: 123957416Smarkm case TCSETSW: 124057416Smarkm case TCSETSF: 124157416Smarkm tsp = (struct termios *) 124257416Smarkm (ibuf+1 + sizeof(struct iocblk)); 124357416Smarkm vstop = tsp->c_cc[VSTOP]; 124457416Smarkm vstart = tsp->c_cc[VSTART]; 124557416Smarkm ixon = tsp->c_iflag & IXON; 124657416Smarkm break; 124757416Smarkm#endif 124857416Smarkm#if 0 124957416Smarkm case TCSETA: 125057416Smarkm case TCSETAW: 125157416Smarkm case TCSETAF: 125257416Smarkm tp = (struct termio *) (ibuf+1 + sizeof(struct iocblk)); 125357416Smarkm vstop = tp->c_cc[VSTOP]; 125457416Smarkm vstart = tp->c_cc[VSTART]; 125557416Smarkm ixon = tp->c_iflag & IXON; 125657416Smarkm break; 125757416Smarkm#endif 125857416Smarkm default: 125957416Smarkm errno = EAGAIN; 126057416Smarkm return(-1); 126157416Smarkm } 126257416Smarkm 126357416Smarkm newflow = (ixon && (vstart == 021) && (vstop == 023)) ? 1 : 0; 126457416Smarkm if (newflow != flowison) { /* it's a change */ 126557416Smarkm flowison = newflow; 126657416Smarkm ibuf[0] = newflow ? TIOCPKT_DOSTOP : TIOCPKT_NOSTOP; 126757416Smarkm return(1); 126857416Smarkm } 126957416Smarkm } 127057416Smarkm 127157416Smarkm /* nothing worth doing anything about */ 127257416Smarkm errno = EAGAIN; 127357416Smarkm return(-1); 127457416Smarkm} 127557416Smarkm#endif /* STREAMSPTY */ 127657416Smarkm 127757416Smarkm/* 127857416Smarkm * Send interrupt to process on other side of pty. 127957416Smarkm * If it is in raw mode, just write NULL; 128057416Smarkm * otherwise, write intr char. 128157416Smarkm */ 128257416Smarkmvoid 128357416Smarkminterrupt() 128457416Smarkm{ 128557416Smarkm ptyflush(); /* half-hearted */ 128657416Smarkm 128757416Smarkm#if defined(STREAMSPTY) && defined(TIOCSIGNAL) 128857416Smarkm /* Streams PTY style ioctl to post a signal */ 128957416Smarkm if (really_stream) 129057416Smarkm { 129157416Smarkm int sig = SIGINT; 129257416Smarkm ioctl(ourpty, TIOCSIGNAL, &sig); 129357416Smarkm ioctl(ourpty, I_FLUSH, FLUSHR); 129457416Smarkm } 129557416Smarkm#else 129657416Smarkm#ifdef TCSIG 129757416Smarkm ioctl(ourpty, TCSIG, (char *)SIGINT); 129857416Smarkm#else /* TCSIG */ 129957416Smarkm init_termbuf(); 130057416Smarkm *pfrontp++ = slctab[SLC_IP].sptr ? 130157416Smarkm (unsigned char)*slctab[SLC_IP].sptr : '\177'; 130257416Smarkm#endif /* TCSIG */ 130357416Smarkm#endif 130457416Smarkm} 130557416Smarkm 130657416Smarkm/* 130757416Smarkm * Send quit to process on other side of pty. 130857416Smarkm * If it is in raw mode, just write NULL; 130957416Smarkm * otherwise, write quit char. 131057416Smarkm */ 131157416Smarkmvoid 131257416Smarkmsendbrk() 131357416Smarkm{ 131457416Smarkm ptyflush(); /* half-hearted */ 131557416Smarkm#ifdef TCSIG 131657416Smarkm ioctl(ourpty, TCSIG, (char *)SIGQUIT); 131757416Smarkm#else /* TCSIG */ 131857416Smarkm init_termbuf(); 131957416Smarkm *pfrontp++ = slctab[SLC_ABORT].sptr ? 132057416Smarkm (unsigned char)*slctab[SLC_ABORT].sptr : '\034'; 132157416Smarkm#endif /* TCSIG */ 132257416Smarkm} 132357416Smarkm 132457416Smarkmvoid 132557416Smarkmsendsusp() 132657416Smarkm{ 132757416Smarkm#ifdef SIGTSTP 132857416Smarkm ptyflush(); /* half-hearted */ 132957416Smarkm# ifdef TCSIG 133057416Smarkm ioctl(ourpty, TCSIG, (char *)SIGTSTP); 133157416Smarkm# else /* TCSIG */ 133257416Smarkm *pfrontp++ = slctab[SLC_SUSP].sptr ? 133357416Smarkm (unsigned char)*slctab[SLC_SUSP].sptr : '\032'; 133457416Smarkm# endif /* TCSIG */ 133557416Smarkm#endif /* SIGTSTP */ 133657416Smarkm} 133757416Smarkm 133857416Smarkm/* 133957416Smarkm * When we get an AYT, if ^T is enabled, use that. Otherwise, 134057416Smarkm * just send back "[Yes]". 134157416Smarkm */ 134257416Smarkmvoid 134357416Smarkmrecv_ayt() 134457416Smarkm{ 134557416Smarkm#if defined(SIGINFO) && defined(TCSIG) 134657416Smarkm if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) { 134757416Smarkm ioctl(ourpty, TCSIG, (char *)SIGINFO); 134857416Smarkm return; 134957416Smarkm } 135057416Smarkm#endif 135157416Smarkm output_data("\r\n[Yes]\r\n"); 135257416Smarkm} 135357416Smarkm 135457416Smarkmvoid 135557416Smarkmdoeof() 135657416Smarkm{ 135757416Smarkm init_termbuf(); 135857416Smarkm 135957416Smarkm *pfrontp++ = slctab[SLC_EOF].sptr ? 136057416Smarkm (unsigned char)*slctab[SLC_EOF].sptr : '\004'; 136157416Smarkm} 1362