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