?? telnetd.c
字號:
/* NOTREACHED */} /* end of main */ voidusage(){ fprintf(stderr, "Usage: telnetd");#ifdef AUTHENTICATION fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t");#endif#ifdef BFTPDAEMON fprintf(stderr, " [-B]");#endif fprintf(stderr, " [-debug]");#ifdef DIAGNOSTICS fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t");#endif#ifdef AUTHENTICATION fprintf(stderr, " [-edebug]");#endif fprintf(stderr, " [-h]");#if defined(CRAY) && defined(NEWINIT) fprintf(stderr, " [-Iinitid]");#endif#if defined(LINEMODE) && defined(KLUDGELINEMODE) fprintf(stderr, " [-k]");#endif#ifdef LINEMODE fprintf(stderr, " [-l]");#endif fprintf(stderr, " [-n]");#ifdef CRAY fprintf(stderr, " [-r[lowpty]-[highpty]]");#endif fprintf(stderr, "\n\t");#ifdef SecurID fprintf(stderr, " [-s]");#endif#ifdef HAS_GETTOS fprintf(stderr, " [-S tos]");#endif#ifdef AUTHENTICATION fprintf(stderr, " [-X auth-type]");#endif fprintf(stderr, " [-u utmp_hostname_length] [-U]"); fprintf(stderr, " [port]\n"); exit(1);}/* * getterminaltype * * Ask the other end to send along its terminal type and speed. * Output is the variable terminaltype filled in. */static unsigned char ttytype_sbbuf[] = { IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE}; intgetterminaltype(name) char *name;{ int retval = -1; void _gettermname(); settimer(baseline);#if defined(AUTHENTICATION) /* * Handle the Authentication option before we do anything else. */ send_do(TELOPT_AUTHENTICATION, 1); while (his_will_wont_is_changing(TELOPT_AUTHENTICATION)) ttloop(); if (his_state_is_will(TELOPT_AUTHENTICATION)) { retval = auth_wait(name); }#endif#ifdef ENCRYPTION send_will(TELOPT_ENCRYPT, 1);#endif /* ENCRYPTION */ send_do(TELOPT_TTYPE, 1); send_do(TELOPT_TSPEED, 1); send_do(TELOPT_XDISPLOC, 1); send_do(TELOPT_NEW_ENVIRON, 1); send_do(TELOPT_OLD_ENVIRON, 1); while (#ifdef ENCRYPTION his_do_dont_is_changing(TELOPT_ENCRYPT) ||#endif /* ENCRYPTION */ his_will_wont_is_changing(TELOPT_TTYPE) || his_will_wont_is_changing(TELOPT_TSPEED) || his_will_wont_is_changing(TELOPT_XDISPLOC) || his_will_wont_is_changing(TELOPT_NEW_ENVIRON) || his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) { ttloop(); }#ifdef ENCRYPTION /* * Wait for the negotiation of what type of encryption we can * send with. If autoencrypt is not set, this will just return. */ if (his_state_is_will(TELOPT_ENCRYPT)) { encrypt_wait(); }#endif /* ENCRYPTION */ if (his_state_is_will(TELOPT_TSPEED)) { static unsigned char sb[] = { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE }; bcopy(sb, nfrontp, sizeof sb); nfrontp += sizeof sb; } if (his_state_is_will(TELOPT_XDISPLOC)) { static unsigned char sb[] = { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE }; bcopy(sb, nfrontp, sizeof sb); nfrontp += sizeof sb; } if (his_state_is_will(TELOPT_NEW_ENVIRON)) { static unsigned char sb[] = { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE }; bcopy(sb, nfrontp, sizeof sb); nfrontp += sizeof sb; } else if (his_state_is_will(TELOPT_OLD_ENVIRON)) { static unsigned char sb[] = { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE }; bcopy(sb, nfrontp, sizeof sb); nfrontp += sizeof sb; } if (his_state_is_will(TELOPT_TTYPE)) { bcopy(ttytype_sbbuf, nfrontp, sizeof ttytype_sbbuf); nfrontp += sizeof ttytype_sbbuf; } if (his_state_is_will(TELOPT_TSPEED)) { while (sequenceIs(tspeedsubopt, baseline)) ttloop(); } if (his_state_is_will(TELOPT_XDISPLOC)) { while (sequenceIs(xdisplocsubopt, baseline)) ttloop(); } if (his_state_is_will(TELOPT_NEW_ENVIRON)) { while (sequenceIs(environsubopt, baseline)) ttloop(); } if (his_state_is_will(TELOPT_OLD_ENVIRON)) { while (sequenceIs(oenvironsubopt, baseline)) ttloop(); } if (his_state_is_will(TELOPT_TTYPE)) { char first[256], last[256]; while (sequenceIs(ttypesubopt, baseline)) ttloop(); /* * If the other side has already disabled the option, then * we have to just go with what we (might) have already gotten. */ if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) { (void) strncpy(first, terminaltype, sizeof(first)); for(;;) { /* * Save the unknown name, and request the next name. */ (void) strncpy(last, terminaltype, sizeof(last)); _gettermname(); if (terminaltypeok(terminaltype)) break; if ((strncmp(last, terminaltype, sizeof(last)) == 0) || his_state_is_wont(TELOPT_TTYPE)) { /* * We've hit the end. If this is the same as * the first name, just go with it. */ if (strncmp(first, terminaltype, sizeof(first)) == 0) break; /* * Get the terminal name one more time, so that * RFC1091 compliant telnets will cycle back to * the start of the list. */ _gettermname(); if (strncmp(first, terminaltype, sizeof(first)) != 0) (void) strncpy(terminaltype, first, sizeof(first)); break; } } } } return(retval);} /* end of getterminaltype */ void_gettermname(){ /* * If the client turned off the option, * we can't send another request, so we * just return. */ if (his_state_is_wont(TELOPT_TTYPE)) return; settimer(baseline); bcopy(ttytype_sbbuf, nfrontp, sizeof ttytype_sbbuf); nfrontp += sizeof ttytype_sbbuf; while (sequenceIs(ttypesubopt, baseline)) ttloop();} intterminaltypeok(s) char *s;{ char buf[1024]; if (terminaltype == NULL) return(1); /* * tgetent() will return 1 if the type is known, and * 0 if it is not known. If it returns -1, it couldn't * open the database. But if we can't open the database, * it won't help to say we failed, because we won't be * able to verify anything else. So, we treat -1 like 1. */ if (tgetent(buf, s) == 0) return(0); return(1);}#ifndef MAXHOSTNAMELEN#define MAXHOSTNAMELEN 64#endif /* MAXHOSTNAMELEN */char *hostname;char host_name[MAXHOSTNAMELEN];char remote_host_name[MAXHOSTNAMELEN];#ifndef convexextern void telnet P((int, int));#elseextern void telnet P((int, int, char *));#endif/* * Get a pty, scan input lines. */doit(who) struct sockaddr_in *who;{ char *host, *inet_ntoa(); int t; struct hostent *hp; int level; int ptynum; char user_name[256]; /* * Find an available pty to use. */#ifndef convex pty = getpty(&ptynum); if (pty < 0) fatal(net, "All network ports in use");#else for (;;) { char *lp; extern char *line, *getpty(); if ((lp = getpty()) == NULL) fatal(net, "Out of ptys"); if ((pty = open(lp, 2)) >= 0) { strcpy(line,lp); line[5] = 't'; break; } }#endif#if defined(_SC_CRAY_SECURE_SYS) /* * set ttyp line security label */ if (secflag) { char slave_dev[16]; sprintf(tty_dev, "/dev/pty/%03d", ptynum); if (setdevs(tty_dev, &dv) < 0) fatal(net, "cannot set pty security"); sprintf(slave_dev, "/dev/ttyp%03d", ptynum); if (setdevs(slave_dev, &dv) < 0) fatal(net, "cannot set tty security"); }#endif /* _SC_CRAY_SECURE_SYS */ /* get name of connected client */ hp = gethostbyaddr((char *)&who->sin_addr, sizeof (struct in_addr), who->sin_family); if (hp == NULL && registerd_host_only) { fatal(net, "Couldn't resolve your address into a host name.\r\n\ Please contact your net administrator"); } else if (hp && (strlen(hp->h_name) <= ((utmp_len < 0) ? -utmp_len : utmp_len))) { host = hp->h_name; } else { host = inet_ntoa(who->sin_addr); } /* * We must make a copy because Kerberos is probably going * to also do a gethost* and overwrite the static data... */ strncpy(remote_host_name, host, sizeof(remote_host_name)-1); remote_host_name[sizeof(remote_host_name)-1] = 0; host = remote_host_name; (void) gethostname(host_name, sizeof (host_name)); hostname = host_name;#if defined(AUTHENTICATION) || defined(ENCRYPTION) auth_encrypt_init(hostname, host, "TELNETD", 1);#endif init_env(); /* * get terminal type. */ *user_name = 0; level = getterminaltype(user_name); setenv("TERM", terminaltype ? terminaltype : "network", 1); /* * Start up the login process on the slave side of the terminal */#ifndef convex startslave(host, level, user_name);#if defined(_SC_CRAY_SECURE_SYS) if (secflag) { if (setulvl(dv.dv_actlvl) < 0) fatal(net,"cannot setulvl()"); if (setucmp(dv.dv_actcmp) < 0) fatal(net, "cannot setucmp()"); }#endif /* _SC_CRAY_SECURE_SYS */ telnet(net, pty); /* begin server processing */#else telnet(net, pty, host);#endif /*NOTREACHED*/} /* end of doit */#if defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50) intXterm_output(ibufp, obuf, icountp, ocount) char **ibufp, *obuf; int *icountp, ocount;{ int ret; ret = term_output(*ibufp, obuf, *icountp, ocount); *ibufp += *icountp; *icountp = 0; return(ret);}#define term_output Xterm_output#endif /* defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50) *//* * Main loop. Select from pty and network, and * hand data to telnet receiver finite state machine. */ void#ifndef convextelnet(f, p)#elsetelnet(f, p, host)#endif int f, p;#ifdef convex char *host;#endif{ int on = 1;#define TABBUFSIZ 512 char defent[TABBUFSIZ]; char defstrs[TABBUFSIZ];#undef TABBUFSIZ char *HE; char *HN; char *IM; void netflush(); /* * Initialize the slc mapping table. */ get_slc_defaults(); /* * Do some tests where it is desireable to wait for a response. * Rather than doing them slowly, one at a time, do them all * at once. */ if (my_state_is_wont(TELOPT_SGA)) send_will(TELOPT_SGA, 1); /* * Is the client side a 4.2 (NOT 4.3) system? We need to know this * because 4.2 clients are unable to deal with TCP urgent data. * * To find out, we send out a "DO ECHO". If the remote system * answers "WILL ECHO" it is probably a 4.2 client, and we note * that fact ("WILL ECHO" ==> that the client will echo what * WE, the server, sends it; it does NOT mean that the client will * echo the terminal input). */ send_do(TELOPT_ECHO, 1);#ifdef LINEMODE if (his_state_is_wont(TELOPT_LINEMODE)) { /* Query the peer for linemode support by trying to negotiate * the linemode option. */ linemode = 0; editmode = 0; send_do(TELOPT_LINEMODE, 1); /* send do linemode */ }#endif /* LINEMODE */ /* * Send along a couple of other options that we wish to negotiate. */ send_do(TELOPT_NAWS, 1); send_will(TELOPT_STATUS, 1); flowmode = 1; /* default flow control state */ restartany = -1; /* uninitialized... */ send_do(TELOPT_LFLOW, 1); /* * Spin, waiting for a response from the DO ECHO. However, * some REALLY DUMB telnets out there might not respond * to the DO ECHO. So, we spin looking for NAWS, (most dumb * telnets so far seem to respond with WONT for a DO that * they don't understand...) because by the time we get the * response, it will already have processed the DO ECHO. * Kludge upon kludge. */ while (his_will_wont_is_changing(TELOPT_NAWS)) ttloop(); /* * But... * The client might have sent a WILL NAWS as part of its * startup code; if so, we'll be here before we get the * response to the DO ECHO. We'll make the assumption * that any implementation that understands about NAWS * is a modern enough implementation that it will respond * to our DO ECHO request; hence we'll do another spin * waiting for the ECHO option to settle down, which is * what we wanted to do in the first place... */ if (his_want_state_is_will(TELOPT_ECHO) && his_state_is_will(TELOPT_NAWS)) { while (his_will_wont_is_changing(TELOPT_ECHO)) ttloop(); } /* * On the off chance that the telnet client is broken and does not * respond to the DO ECHO we sent, (after all, we did send the * DO NAWS negotiation after the DO ECHO, and we won't get here * until a response to the DO NAWS comes back) simulate the * receipt of a will echo. This will also send a WONT ECHO * to the client, since we assume that the client failed to * respond because it believes that it is already in DO ECHO * mode, which we do not want. */ if (his_want_state_is_will(TELOPT_ECHO)) { DIAG(TD_OPTIONS, {sprintf(nfrontp, "td: simulating recv\r\n"); nfrontp += strlen(nfrontp);}); willoption(TELOPT_ECHO); } /* * Finally, to clean things up, we turn on our echo. This * will break stupid 4.2 telnets out of local terminal echo. */ if (my_state_is_wont(TELOPT_ECHO)) send_will(TELOPT_ECHO, 1);#ifndef STREAMSPTY /* * Turn on packet mode */ (void) ioctl(p, TIOCPKT, (char *)&on);#endif#if defined(LINEMODE) && defined(KLUDGELINEMODE) /* * Continuing line mode support. If client does not support * real linemode, attempt to negotiate kludge linemode by sending * the do timing mark sequence. */ if (lmodetype < REAL_LINEMODE) send_do(TELOPT_TM, 1);#endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */ /* * Call telrcv() once to pick up anything received during * terminal type negotiation, 4.2/4.3 determination, and * linemode negotiation. */ telrcv(); (void) ioctl(f, FIONBIO, (char *)&on); (void) ioctl(p, FIONBIO, (char *)&on);#if defined(CRAY2) && defined(UNICOS5) init_termdriver(f, p, interrupt, sendbrk);#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -