📄 rsh.c
字号:
/*- * Copyright (c) 1983, 1990, 1993, 1994, 2002 * 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. * 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. */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <sys/types.h>#include <sys/socket.h>#include <sys/ioctl.h>#ifdef HAVE_SYS_FILIO_H# include <sys/filio.h>#endif#include <sys/file.h>#ifdef TIME_WITH_SYS_TIME# include <sys/time.h># include <time.h>#else# ifdef HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#include <netinet/in.h>#include <netdb.h>#include <error.h>#include <errno.h>#include <pwd.h>#include <signal.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__# include <stdarg.h>#else# include <varargs.h>#endif#include <getopt.h>#ifdef HAVE_SYS_SELECT_H# include <sys/select.h>#endif#ifdef SHISHI#define REALM_SZ 1040#endif#if defined(KERBEROS) || defined(SHISHI)int use_kerberos = 1, doencrypt;#ifdef KERBEROS#include <kerberosIV/des.h>#include <kerberosIV/krb.h>char dest_realm_buf[REALM_SZ], *dest_realm = NULL;CREDENTIALS cred;Key_schedule schedule;extern char *krb_realmofhost();# elif defined(SHISHI)# include <shishi.h># include "shishi_def.h"char dest_realm_buf[REALM_SZ], *dest_realm = NULL;Shishi * h;Shishi_key * enckey;shishi_ivector iv1, iv2, iv3, iv4;shishi_ivector * ivtab[4];int keytype;int keylen;int rc;int wlen;#endif#endif /* KERBEROS *//* * rsh - remote shell */int rfd2;char *copyargs (char **);RETSIGTYPE sendsig (int);void talk (int, sigset_t *, pid_t, int);void usage (void);void warning (const char *, ...);char *program_name;#ifdef KERBEROS#ifdef ENCRYPTION#define OPTIONS "8Kdek:l:nxVh"#else#define OPTIONS "8Kdek:l:nVh"#endif#else#define OPTIONS "8KLdel:nVh"#endifstatic const char *short_options = "+" OPTIONS;static struct option long_options[] ={ { "debug", no_argument, 0, 'd' }, { "user", required_argument, 0, 'l' }, { "escape", required_argument, 0, 'e' }, { "8-bit", no_argument, 0, '8' }, { "kerberos", no_argument, 0, 'K' }, { "no-input", no_argument, 0, 'n' },#if defined(KERBEROS) || defined(SHISHI) { "realm", required_argument, 0, 'k' }, { "encrypt", no_argument, 0, 'x' },#endif { "help", no_argument, 0, 'h' }, { "version", no_argument, 0, 'V' }, { 0, 0, 0, 0 }};static voidpusage (FILE *stream){ fprintf (stream, "Usage: %s [-nd%s]%s[-l USER] [USER@]HOST [COMMAND [ARG...]]\n", program_name,#if defined(KERBEROS) || defined(SHISHI)#ifdef ENCRYPTION "x", " [-k REALM] "#else "", " [-k REALM] "#endif#else "", " "#endif );}/* Print a help message describing all options to STDOUT and exit with a status of 0. */static voidhelp (void){ pusage (stdout); puts ("Execute COMMAND on remote system HOST"); puts ("When use as rlogin:"); puts ("\ -8, --8-bit allows an eight-bit input data path at all times"); puts ("\ -E, --no-escape stops any character from being recognized as an escape\n\ character"); puts ("\ -d, --debug turns on socket debugging (see setsockopt(2))"); puts ("\ -e, --escape=CHAR allows user specification of the escape character,\n\ which is ``~'' by default"); puts ("\ -l, --user USER run as USER on the remote system");#if defined(KERBEROS) || defined(SHISHI) puts ("\ -K, --kerberos turns off all Kerberos authentication"); puts ("\ -k, --realm REALM Obtain tickets for the remote host in REALM\n\ instead of the remote host's realm");# ifdef ENCRYPTION puts ("\ -x, --encrypt encrypt all data using DES");# endif#endif puts ("\ -n, --no-input use /dev/null as input"); puts ("\ --help give this help list"); puts ("\ -V, --version print program version"); fprintf (stdout, "\nSubmit bug reports to %s.\n", PACKAGE_BUGREPORT); exit (0);}/* Print a message saying to use --help to STDERR, and exit with a status of 1. */static voidtry_help (void){ fprintf (stderr, "Try `%s --help' for more information.\n", program_name); exit (1);}voidusage(){ pusage (stderr); try_help ();}intmain (int argc, char **argv){ struct passwd *pw; struct servent *sp; sigset_t sigs, osigs; int asrsh, ch, dflag, nflag, rem; pid_t pid = 0; uid_t uid; char *args, *host, *user; program_name = argv[0]; asrsh = dflag = nflag = 0; host = user = NULL; /* If called as something other than "rsh", use it as the host name */ { char *p = strrchr(argv[0], '/'); if (p) ++p; else p = argv[0]; if (strcmp (p, "rsh")) host = p; else asrsh = 1; } while ((ch = getopt_long (argc, argv, short_options, long_options, 0)) != EOF) { switch (ch) { case 'L': /* -8Lew are ignored to allow rlogin aliases */ case 'e': case 'w': case '8': break; case 'd': dflag = 1; break; case 'l': user = optarg; break; case 'K':#if defined(KERBEROS) || defined(SHISHI) use_kerberos = 0;#endif break;#if defined(KERBEROS) || defined(SHISHI) case 'k': strncpy (dest_realm_buf, optarg, sizeof dest_realm_buf); dest_realm_buf [REALM_SZ - 1] = '\0'; dest_realm = dest_realm_buf; break;# ifdef ENCRYPTION case 'x': doencrypt = 1;#ifdef KERBEROS des_set_key (cred.session, schedule);#endif break;# endif#endif case 'n': nflag = 1; break; case 'h': help (); case 'V': printf ("rsh (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION); exit (0); case '?': try_help (); default: usage(); } } if (optind < argc) host = argv[optind++]; /* To few args. */ if (!host) usage (); /* If no further arguments, must have been called as rlogin. */ if (!argv[optind]) { if (asrsh) *argv = (char *)"rlogin"; seteuid (getuid ()); setuid (getuid ()); execv (PATH_RLOGIN, argv); error (1, errno, "can't exec %s", PATH_RLOGIN); } argc -= optind; argv += optind; /* We must be setuid root. */ if (geteuid ()) error (1, 0, "must be setuid root.\n"); if (!(pw = getpwuid (uid = getuid ()))) error (1, 0, "unknown user id"); /* Accept user1@host format, though "-l user2" overrides user1 */ { char *p = strchr (host, '@'); if (p) { *p = '\0'; if (!user && p > host) user = host; host = p + 1; if (*host == '\0') usage (); } }#if defined(KERBEROS) || defined(SHISHI)#ifdef ENCRYPTION /* -x turns off -n */ if (doencrypt) nflag = 0;#endif#endif args = copyargs (argv); sp = NULL;#ifdef KERBEROS if (use_kerberos) { sp = getservbyname ((doencrypt ? "ekshell" : "kshell"), "tcp"); if (sp == NULL) { use_kerberos = 0; warning ("can't get entry for %s/tcp service", doencrypt ? "ekshell" : "kshell"); } }#elif defined(SHISHI) if (use_kerberos) { sp = getservbyname ("kshell", "tcp"); if (sp == NULL) { use_kerberos = 0; warning ("can't get entry for %s/tcp service", "kshell"); } }#endif if (sp == NULL) sp = getservbyname("shell", "tcp"); if (sp == NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -