📄 rlogind.c
字号:
/*- * Copyright (c) 1983, 1988, 1989, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#ifndef lintstatic char copyright[] ="@(#) Copyright (c) 1983, 1988, 1989, 1993\n\ The Regents of the University of California. All rights reserved.\n";#endif /* not lint */#ifndef lintstatic char sccsid[] = "@(#)rlogind.c 8.1 (Berkeley) 6/4/93";#endif /* not lint *//* * remote login server: * \0 * remuser\0 * locuser\0 * terminal_type/speed\0 * data */#define FD_SETSIZE 16 /* don't need many bits for select */#include <sys/param.h>#include <sys/stat.h>#include <sys/ioctl.h>#include <signal.h>#include <termios.h>#include <sys/socket.h>#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/ip.h>#include <arpa/inet.h>#include <netdb.h>#include <pwd.h>#include <syslog.h>#include <errno.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include "pathnames.h"#ifndef TIOCPKT_WINDOW#define TIOCPKT_WINDOW 0x80#endif#ifdef KERBEROS#include <kerberosIV/des.h>#include <kerberosIV/krb.h>#define SECURE_MESSAGE "This rlogin session is using DES encryption for all transmissions.\r\n"AUTH_DAT *kdata;KTEXT ticket;u_char auth_buf[sizeof(AUTH_DAT)];u_char tick_buf[sizeof(KTEXT_ST)];Key_schedule schedule;int doencrypt, retval, use_kerberos, vacuous;#define ARGSTR "alnkvx"#else#define ARGSTR "aln"#endif /* KERBEROS */char *env[2];#define NMAX 30char lusername[NMAX+1], rusername[NMAX+1];static char term[64] = "TERM=";#define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */int keepalive = 1;int check_all = 0;struct passwd *pwd;void doit __P((int, struct sockaddr_in *));int control __P((int, char *, int));void protocol __P((int, int));void cleanup __P((int));void fatal __P((int, char *, int));int do_rlogin __P((struct sockaddr_in *));void getstr __P((char *, int, char *));void setup_term __P((int));int do_krb_login __P((struct sockaddr_in *));void usage __P((void));int local_domain __P((char *));char *topdomain __P((char *));intmain(argc, argv) int argc; char *argv[];{ extern int __check_rhosts_file; struct sockaddr_in from; int ch, fromlen, on; openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH); opterr = 0; while ((ch = getopt(argc, argv, ARGSTR)) != EOF) switch (ch) { case 'a': check_all = 1; break; case 'l': __check_rhosts_file = 0; break; case 'n': keepalive = 0; break;#ifdef KERBEROS case 'k': use_kerberos = 1; break; case 'v': vacuous = 1; break;#ifdef CRYPT case 'x': doencrypt = 1; break;#endif#endif case '?': default: usage(); break; } argc -= optind; argv += optind;#ifdef KERBEROS if (use_kerberos && vacuous) { usage(); fatal(STDERR_FILENO, "only one of -k and -v allowed", 0); }#endif fromlen = sizeof (from); if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { syslog(LOG_ERR,"Can't get peer name of remote host: %m"); fatal(STDERR_FILENO, "Can't get peer name of remote host", 1); } on = 1; if (keepalive && setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); on = IPTOS_LOWDELAY; if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); doit(0, &from);}int child;int netf;char line[MAXPATHLEN];int confirmed;struct winsize win = { 0, 0, 0, 0 };voiddoit(f, fromp) int f; struct sockaddr_in *fromp;{ int master, pid, on = 1; int authenticated = 0; register struct hostent *hp; char hostname[2 * MAXHOSTNAMELEN + 1]; char c; alarm(60); read(f, &c, 1); if (c != 0) exit(1);#ifdef KERBEROS if (vacuous) fatal(f, "Remote host requires Kerberos authentication", 0);#endif alarm(0); fromp->sin_port = ntohs((u_short)fromp->sin_port); hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof(struct in_addr), fromp->sin_family); if (hp) (void)strcpy(hostname, hp->h_name); else (void)strcpy(hostname, inet_ntoa(fromp->sin_addr));#ifdef KERBEROS if (use_kerberos) { retval = do_krb_login(fromp); if (retval == 0) authenticated++; else if (retval > 0) fatal(f, krb_err_txt[retval], 0); write(f, &c, 1); confirmed = 1; /* we sent the null! */ } else#endif { if (fromp->sin_family != AF_INET || fromp->sin_port >= IPPORT_RESERVED || fromp->sin_port < IPPORT_RESERVED/2) { syslog(LOG_NOTICE, "Connection from %s on illegal port", inet_ntoa(fromp->sin_addr)); fatal(f, "Permission denied", 0); }#ifdef IP_OPTIONS { u_char optbuf[BUFSIZ/3], *cp; char lbuf[BUFSIZ], *lp; int optsize = sizeof(optbuf), ipproto; struct protoent *ip; if ((ip = getprotobyname("ip")) != NULL) ipproto = ip->p_proto; else ipproto = IPPROTO_IP; if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) == 0 && optsize != 0) { lp = lbuf; for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3) sprintf(lp, " %2.2x", *cp); syslog(LOG_NOTICE, "Connection received using IP options (ignored):%s", lbuf); if (setsockopt(0, ipproto, IP_OPTIONS, (char *)NULL, optsize) != 0) { syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m"); exit(1); } } }#endif if (do_rlogin(fromp) == 0) authenticated++; } if (confirmed == 0) { write(f, "", 1); confirmed = 1; /* we sent the null! */ }#ifdef KERBEROS#ifdef CRYPT if (doencrypt) (void) des_write(f, SECURE_MESSAGE, sizeof(SECURE_MESSAGE) - 1);#endif#endif netf = f; pid = forkpty(&master, line, NULL, &win); if (pid < 0) { if (errno == ENOENT) fatal(f, "Out of ptys", 0); else fatal(f, "Forkpty", 1); } if (pid == 0) { if (f > 2) /* f should always be 0, but... */ (void) close(f); setup_term(0); if (authenticated) {#ifdef KERBEROS if (use_kerberos && (pwd->pw_uid == 0)) syslog(LOG_INFO|LOG_AUTH, "ROOT Kerberos login from %s.%s@%s on %s\n", kdata->pname, kdata->pinst, kdata->prealm, hostname);#endif execl(_PATH_LOGIN, "login", "-p", "-h", hostname, "-f", lusername, (char *)NULL); } else execl(_PATH_LOGIN, "login", "-p", "-h", hostname, lusername, (char *)NULL); fatal(STDERR_FILENO, _PATH_LOGIN, 1); /*NOTREACHED*/ }#ifdef CRYPT#ifdef KERBEROS /* * If encrypted, don't turn on NBIO or the des read/write * routines will croak. */ if (!doencrypt)#endif#endif ioctl(f, FIONBIO, &on); ioctl(master, FIONBIO, &on); ioctl(master, TIOCPKT, &on); signal(SIGCHLD, cleanup); protocol(f, master); signal(SIGCHLD, SIG_IGN); cleanup(0);}char magic[2] = { 0377, 0377 };char oobdata[] = {TIOCPKT_WINDOW};/* * Handle a "control" request (signaled by magic being present) * in the data stream. For now, we are only willing to handle * window size changes. */intcontrol(pty, cp, n) int pty; char *cp; int n;{ struct winsize w; if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's') return (0); oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */ bcopy(cp+4, (char *)&w, sizeof(w)); w.ws_row = ntohs(w.ws_row); w.ws_col = ntohs(w.ws_col); w.ws_xpixel = ntohs(w.ws_xpixel); w.ws_ypixel = ntohs(w.ws_ypixel); (void)ioctl(pty, TIOCSWINSZ, &w); return (4+sizeof (w));}/* * rlogin "protocol" machine. */voidprotocol(f, p) register int f, p;{ char pibuf[1024+1], fibuf[1024], *pbp, *fbp; register pcc = 0, fcc = 0; int cc, nfd, n; char cntl; /* * Must ignore SIGTTOU, otherwise we'll stop * when we try and set slave pty's window shape * (our controlling tty is the master pty). */ (void) signal(SIGTTOU, SIG_IGN); send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */ if (f > p) nfd = f + 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -