📄 commands.c
字号:
/* clear the socket address prior to use */ memset((char *)&sin, 0, sizeof(sin)); if (connected) { printf("?Already connected to %s\n", hostname); setuid(getuid()); return 0; } if (argc < 2) { (void) strcpy(line, "open "); printf("(to) "); (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin); makeargv(); argc = margc; argv = margv; } cmd = *argv; --argc; ++argv; while (argc) { if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?")) goto usage; if (strcmp(*argv, "-l") == 0) { --argc; ++argv; if (argc == 0) goto usage; user = *argv++; --argc; continue; } if (strcmp(*argv, "-a") == 0) { --argc; ++argv; autologin = 1; continue; } if (hostp == 0) { hostp = *argv++; --argc; continue; } if (portp == 0) { portp = *argv++; --argc; continue; } usage: printf("usage: %s [-l user] [-a] host-name [port]\n", cmd); setuid(getuid()); return 0; } if (hostp == 0) goto usage;#if defined(IP_OPTIONS) && defined(IPPROTO_IP) if (hostp[0] == '@' || hostp[0] == '!') { if ((hostname = strrchr(hostp, ':')) == NULL) hostname = strrchr(hostp, '@'); hostname++; srp = 0; temp = sourceroute(hostp, &srp, &srlen); if (temp == 0) { herror(srp); setuid(getuid()); return 0; } else if (temp == -1) { printf("Bad source route option: %s\n", hostp); setuid(getuid()); return 0; } else { sin.sin_addr.s_addr = temp; sin.sin_family = AF_INET; } } else {#endif temp = inet_addr(hostp); if (temp != (unsigned long) -1) { sin.sin_addr.s_addr = temp; sin.sin_family = AF_INET; if (_hostname) free (_hostname); _hostname = malloc (strlen (hostp) + 1); if (_hostname) { strcpy (_hostname, hostp); hostname = _hostname; } else { printf ("Can't allocate memory to copy hostname\n"); setuid(getuid()); return 0; } } else { host = gethostbyname(hostp); if (host) { sin.sin_family = host->h_addrtype;#if defined(h_addr) /* In 4.3, this is a #define */ memmove((caddr_t)&sin.sin_addr, host->h_addr_list[0], host->h_length);#else /* defined(h_addr) */ memmove((caddr_t)&sin.sin_addr, host->h_addr, host->h_length);#endif /* defined(h_addr) */ if (_hostname) free (_hostname); _hostname = malloc (strlen (host->h_name) + 1); if (_hostname) { strcpy (_hostname, host->h_name); hostname = _hostname; } else { printf ("Can't allocate memory to copy hostname\n"); setuid(getuid()); return 0; } } else { herror(hostp); setuid(getuid()); return 0; } }#if defined(IP_OPTIONS) && defined(IPPROTO_IP) }#endif if (portp) { if (*portp == '-') { portp++; telnetport = 1; } else telnetport = 0; sin.sin_port = atoi(portp); if (sin.sin_port == 0) { sp = getservbyname(portp, "tcp"); if (sp) sin.sin_port = sp->s_port; else { printf("%s: bad port number\n", portp); setuid(getuid()); return 0; } } else {#if !HAVE_DECL_HTONS#ifndef htons u_short htons __P((unsigned short));#endif#endif sin.sin_port = htons (sin.sin_port); } } else { if (sp == 0) { sp = getservbyname("telnet", "tcp"); if (sp == 0) { fprintf(stderr, "telnet: tcp/telnet: unknown service\n"); setuid(getuid()); return 0; } sin.sin_port = sp->s_port; } telnetport = 1; } printf("Trying %s...\n", inet_ntoa(sin.sin_addr)); do { net = socket(AF_INET, SOCK_STREAM, 0); setuid(getuid()); if (net < 0) { perror("telnet: socket"); return 0; }#if defined(IP_OPTIONS) && defined(IPPROTO_IP) if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0) perror("setsockopt (IP_OPTIONS)");#endif#if defined(IPPROTO_IP) && defined(IP_TOS) {# if defined(HAS_GETTOS) struct tosent *tp; if (tos < 0 && (tp = gettosbyname("telnet", "tcp"))) tos = tp->t_tos;# endif if (tos < 0) tos = 020; /* Low Delay bit */ if (tos && (setsockopt(net, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) && (errno != ENOPROTOOPT)) perror("telnet: setsockopt (IP_TOS) (ignored)"); }#endif /* defined(IPPROTO_IP) && defined(IP_TOS) */ if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) { perror("setsockopt (SO_DEBUG)"); } if (connect(net, (struct sockaddr *)&sin, sizeof (sin)) < 0) {#if defined(h_addr) /* In 4.3, this is a #define */ if (host && host->h_addr_list[1]) { int oerrno = errno; fprintf(stderr, "telnet: connect to address %s: ", inet_ntoa(sin.sin_addr)); errno = oerrno; perror((char *)0); host->h_addr_list++; memmove((caddr_t)&sin.sin_addr, host->h_addr_list[0], host->h_length); (void) NetClose(net); continue; }#endif /* defined(h_addr) */ perror("telnet: Unable to connect to remote host"); return 0; } connected++;#if defined(AUTHENTICATION) || defined(ENCRYPTION) auth_encrypt_connect(connected);#endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */ } while (connected == 0); cmdrc(hostp, hostname); if (autologin && user == NULL) { struct passwd *pw; user = getenv("USER"); if (user == NULL || (pw = getpwnam(user)) && pw->pw_uid != getuid()) { if (pw = getpwuid(getuid())) user = pw->pw_name; else user = NULL; } } if (user) { env_define((unsigned char *)"USER", (unsigned char *)user); env_export((unsigned char *)"USER"); } (void) call(status, "status", "notmuch", 0); if (setjmp(peerdied) == 0) telnet(user); (void) NetClose(net); ExitString("Connection closed by foreign host.\n",1); /*NOTREACHED*/}#define HELPINDENT (sizeof ("connect"))static char openhelp[] = "connect to a site", closehelp[] = "close current connection", logouthelp[] = "forcibly logout remote user and close the connection", quithelp[] = "exit telnet", statushelp[] = "print status information", helphelp[] = "print help information", sendhelp[] = "transmit special characters ('send ?' for more)", sethelp[] = "set operating parameters ('set ?' for more)", unsethelp[] = "unset operating parameters ('unset ?' for more)", togglestring[] ="toggle operating parameters ('toggle ?' for more)", slchelp[] = "change state of special charaters ('slc ?' for more)", displayhelp[] = "display operating parameters",#if defined(TN3270) && defined(unix) transcomhelp[] = "specify Unix command for transparent mode pipe",#endif /* defined(TN3270) && defined(unix) */#if defined(AUTHENTICATION) authhelp[] = "turn on (off) authentication ('auth ?' for more)",#endif#ifdef ENCRYPTION encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",#endif /* ENCRYPTION */#if defined(unix) zhelp[] = "suspend telnet",#endif /* defined(unix) */ shellhelp[] = "invoke a subshell", envhelp[] = "change environment variables ('environ ?' for more)", modestring[] = "try to enter line or character mode ('mode ?' for more)";static int help();static Command cmdtab[] = { { "close", closehelp, bye, 1 }, { "logout", logouthelp, logout, 1 }, { "display", displayhelp, display, 0 }, { "mode", modestring, modecmd, 0 }, { "open", openhelp, tn, 0 }, { "quit", quithelp, quit, 0 }, { "send", sendhelp, sendcmd, 0 }, { "set", sethelp, setcmd, 0 }, { "unset", unsethelp, unsetcmd, 0 }, { "status", statushelp, status, 0 }, { "toggle", togglestring, toggle, 0 }, { "slc", slchelp, slccmd, 0 },#if defined(TN3270) && defined(unix) { "transcom", transcomhelp, settranscom, 0 },#endif /* defined(TN3270) && defined(unix) */#if defined(AUTHENTICATION) { "auth", authhelp, auth_cmd, 0 },#endif#ifdef ENCRYPTION { "encrypt", encrypthelp, encrypt_cmd, 0 },#endif /* ENCRYPTION */#if defined(unix) { "z", zhelp, suspend, 0 },#endif /* defined(unix) */#if defined(TN3270) { "!", shellhelp, shell, 1 },#else { "!", shellhelp, shell, 0 },#endif { "environ", envhelp, env_cmd, 0 }, { "?", helphelp, help, 0 }, 0};static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";static char escapehelp[] = "deprecated command -- use 'set escape' instead";static Command cmdtab2[] = { { "help", 0, help, 0 }, { "escape", escapehelp, setescape, 0 }, { "crmod", crmodhelp, togcrmod, 0 }, 0};/* * Call routine with argc, argv set from args (terminated by 0). */ /*VARARGS1*/ static#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__call(intrtn_t routine, ...)#elsecall(va_alist) va_dcl#endif{ va_list ap;#if !(defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__) intrtn_t routine;#endif char *args[100]; int argno = 0;#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__ va_start(ap, routine);#else va_start(ap); routine = (va_arg(ap, intrtn_t));#endif while ((args[argno++] = va_arg(ap, char *)) != 0) { ; } va_end(ap); return (*routine)(argno-1, args);}static Command *getcmd(char *name){ Command *cm; if (cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))) return cm; return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));}voidcommand(int top, char *tbuf, int cnt){ register Command *c; setcommandmode(); if (!top) { putchar('\n');#if defined(unix) } else { (void) signal(SIGINT, SIG_DFL); (void) signal(SIGQUIT, SIG_DFL);#endif /* defined(unix) */ } for (;;) { if (rlogin == _POSIX_VDISABLE) printf("%s> ", prompt); if (tbuf) { register char *cp; cp = line; while (cnt > 0 && (*cp++ = *tbuf++) != '\n') cnt--; tbuf = 0; if (cp == line || *--cp != '\n' || cp == line) goto getline; *cp = '\0'; if (rlogin == _POSIX_VDISABLE) printf("%s\n", line); } else { getline: if (rlogin != _POSIX_VDISABLE) printf("%s> ", prompt); if (fgets(line, sizeof(line), stdin) == NULL) { if (feof(stdin) || ferror(stdin)) { (void) quit(); /*NOTREACHED*/ } break; } } if (line[0] == 0) break; makeargv(); if (margv[0] == 0) { break; } c = getcmd(margv[0]); if (Ambiguous(c)) { printf("?Ambiguous command\n"); continue; } if (c == 0) { printf("?Invalid command\n"); continue; } if (c->needconnect && !connected) { printf("?Need to be connected first.\n"); continue; } if ((*c->handler)(margc, margv)) { break; } } if (!top) { if (!connected) { longjmp(toplevel, 1); /*NOTREACHED*/ }#if defined(TN3270) if (shell_active == 0) { setconnmode(0); }#else /* defined(TN3270) */ setconnmode(0);#endif /* defined(TN3270) */ }}/* * Help command. */static inthelp(int argc, char *argv[]){ register Command *c; if (argc == 1) { printf("Commands may be abbreviated. Commands are:\n\n"); for (c = cmdtab; c->name; c++) if (c->help) { printf("%-*s\t%s\n", HELPINDENT, c->name, c->help); } return 0; } while (--argc > 0) { register char *arg; arg = *++argv; c = getcmd(arg); if (Ambiguous(c)) printf("?Ambiguous help command %s\n", arg); else if (c == (Command *)0) printf("?Invalid help command %s\n", arg); else printf("%s\n", c->help); } return 0;}static char *rcname = 0;static char rcbuf[128];intcmdrc(char *m1, char *m2){ register Command *c; FILE *rcfile; int gotmachine = 0; int l1 = strlen(m1); int l2 = strlen(m2); char m1save[64]; if (skiprc) return; strcpy(m1save, m1); m1 = m1save; if (rcname == 0) { rcname = getenv("HOME"); if (rcname) strcpy(rcbuf, rcname); else rcbuf[0] = '\0'; strcat(rcbuf, "/.telnetrc"); rcname = rcbuf; } if ((rcfile = fopen(rcname, "r")) == 0) { return; } for (;;) { if (fgets(line, sizeof(line), rcfile) == NULL) break; if (line[0] == 0) break; if (line[0] == '#') continue; if (gotmachine) { if (!isspace(line[0])) gotmachine = 0; } if (gotmachine == 0) { if (isspace(line[0])) continue; if (strncasecmp(line, m1, l1) == 0) strncpy(line, &line[l1], sizeof(line) - l1); else if (strncasecmp(line, m2, l2) == 0) strncpy(line, &line[l2], sizeof(line) - l2); else if (strncasecmp(line, "DEFAULT", 7) == 0) strncpy(line, &line[7], sizeof(line) - 7); else continue; if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n') continue; gotmachine = 1; } makeargv(); if (margv[0] == 0) continue; c = getcmd(margv[0]); if (Ambiguous(c)) { printf("?Ambiguous command: %s\n", margv[0]); continue; } if (c == 0) { printf("?Invalid command: %s\n", margv[0]); continue; } /* * This should never happen... */ if (c->needconnect && !connected) { printf("?Need to be connected first for %s.\n", margv[0]); continue; } (*c->handler)(margc, margv); } fclose(rcfile);}#if defined(IP_OP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -