home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / inetutils-1.2-src.tgz / tar.out / fsf / inetutils / telnet / main.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  10KB  |  409 lines

  1. /*
  2.  * Copyright (c) 1988, 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char copyright[] =
  36. "@(#) Copyright (c) 1988, 1990, 1993\n\
  37.     The Regents of the University of California.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)main.c    8.3 (Berkeley) 5/30/95";
  42. #endif /* not lint */
  43.  
  44. #ifdef HAVE_CONFIG_H
  45. #include <config.h>
  46. #endif
  47.  
  48. #include <sys/types.h>
  49.  
  50. #include <getopt.h>
  51.  
  52. #include "ring.h"
  53. #include "externs.h"
  54. #include "defines.h"
  55.  
  56. #include "version.h"
  57.  
  58. /* These values need to be the same as defined in libtelnet/kerberos5.c */
  59. /* Either define them in both places, or put in some common header file. */
  60. #define OPTS_FORWARD_CREDS           0x00000002
  61. #define OPTS_FORWARDABLE_CREDS       0x00000001
  62.  
  63. #if 0
  64. #define FORWARD
  65. #endif
  66.  
  67. /*
  68.  * Initialize variables.
  69.  */
  70.     void
  71. tninit()
  72. {
  73.     init_terminal();
  74.  
  75.     init_network();
  76.     
  77.     init_telnet();
  78.  
  79.     init_sys();
  80.  
  81. #if defined(TN3270)
  82.     init_3270();
  83. #endif
  84. }
  85.  
  86. #define USAGE "Usage: %s [OPTION...] [HOST [PORT]]\n"
  87.  
  88. /* Print a help message describing all options to STDOUT and exit with a
  89.    status of 0.  */
  90. static void
  91. help ()
  92. {
  93.   fprintf (stdout, USAGE, prompt);
  94.  
  95.   puts ("\n\
  96.   -8, --binary               Use an 8-bit data path\n\
  97.   -a, --login                Attempt automatic login\n\
  98.   -c, --no-rc                Don't read the user's .telnetrc file\n\
  99.   -d, --debug                Turn on debugging\n\
  100.   -e CHAR, --escape=CHAR     Use CHAR as an escape character\n\
  101.   -E, --no-escape            Use no escape character\n\
  102.   -K, --no-login             Don't automatically login to the remote system\n\
  103.   -l USER, --user=USER       Attempt automatic login as USER\n\
  104.   -L, --binary-output        Use an 8-bit data path for output only\n\
  105.   -n FILE, --trace=FILE      Record trace information into FILE\n\
  106.   -r, --rlogin               Use a user-interface similar to rlogin\n\
  107.   -S TOS, --tos=TOS          Use the IP type-of-service TOS\n\
  108.   -X ATYPE, --disable-auth=ATYPE   Disable type ATYPE authentication");
  109.  
  110. #ifdef ENCRYPTION
  111.   puts ("\
  112.   -x, --encrypt              Encrypt the data stream, if possible");
  113. #endif
  114.  
  115. #ifdef AUTHENTICATION
  116.   puts ("\n\
  117.  When using Kerberos authentication:\n\
  118.   -f, --fwd-credentials      Allow the the local credentials to be forwarded\n\
  119.   -k REALM, --realm=REALM    Obtain tickets for the remote host in REALM\n\
  120.                              instead of the remote host's realm");
  121. #endif
  122.  
  123. #if defined(TN3270) && defined(unix)
  124.   puts ("\n\
  125.  TN3270 options (note non-standard option syntax):\n\
  126.       -noasynch\n\
  127.       -noasynctty\n\
  128.       -noasyncnet\n\
  129.   -t LINE, --transcom=LINE");
  130. #endif
  131.  
  132. #if defined (ENCRYPTION) || defined (AUTHENTICATION) || defined (TN3270)
  133.   putc ('\n');
  134. #endif
  135.  
  136.   puts ("\
  137.       --help                 Give this help list\n\
  138.       --version              Print program version");
  139.  
  140.   exit (0);
  141. }
  142.  
  143. /* Print a message saying to use --help to STDERR, and exit with a status of
  144.    1.  */
  145. static void
  146. try_help ()
  147. {
  148.   fprintf (stderr, "Try `%s --help' for more information.\n", prompt);
  149.   exit (1);
  150. }
  151.  
  152. /* Print a usage message to STDERR and exit with a status of 1.  */
  153. static void
  154. usage ()
  155. {
  156.   fprintf (stderr, USAGE, prompt);
  157.   try_help ();
  158. }
  159.  
  160. static struct option long_options[] =
  161. {
  162.   { "binary", no_argument, 0, '8' },
  163.   { "login", no_argument, 0, 'a' },
  164.   { "no-rc", no_argument, 0, 'c' },
  165.   { "debug", no_argument, 0, 'd' },
  166.   { "escape", required_argument, 0, 'e' },
  167.   { "no-escape", no_argument, 0, 'E' },
  168.   { "no-login", no_argument, 0, 'K' },
  169.   { "user", required_argument, 0, 'l' },
  170.   { "binary-output", no_argument, 0, 'L' },
  171.   { "trace", required_argument, 0, 'n' },
  172.   { "rlogin", no_argument, 0, 'r' },
  173.   { "tos", required_argument, 0, 'S' },
  174.   { "disable-auth", required_argument, 0, 'X' },
  175.   { "encrypt", no_argument, 0, 'x' },
  176.   { "fwd-credentials", no_argument, 0, 'f' },
  177.   { "realm", required_argument, 0, 'k' },
  178.   { "transcom", required_argument, 0, 't' },
  179.   { "help", no_argument, 0, '&' },
  180.   { "version", no_argument, 0, 'V' },
  181.   { 0 }
  182. };
  183.  
  184. /*
  185.  * main.  Parse arguments, invoke the protocol or command parser.
  186.  */
  187. main(argc, argv)
  188.     int argc;
  189.     char *argv[];
  190. {
  191.     extern char *optarg;
  192.     extern int optind;
  193.     int ch;
  194.     char *user, *strrchr();
  195. #ifdef    FORWARD
  196.     extern int forward_flags;
  197. #endif    /* FORWARD */
  198.  
  199.     tninit();        /* Clear out things */
  200. #if    defined(CRAY) && !defined(__STDC__)
  201.     _setlist_init();    /* Work around compiler bug */
  202. #endif
  203.  
  204.     TerminalSaveState();
  205.  
  206.     if (prompt = strrchr(argv[0], '/'))
  207.         ++prompt;
  208.     else
  209.         prompt = argv[0];
  210.  
  211.     user = NULL;
  212.  
  213.     rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
  214.     autologin = -1;
  215.  
  216.     while ((ch = getopt_long (argc, argv, "8EKLS:X:acde:fFk:l:n:rt:x",
  217.                   long_options, 0))
  218.            != EOF)
  219.     {
  220.         switch(ch) {
  221.         case '8':
  222.             eight = 3;    /* binary output and input */
  223.             break;
  224.         case 'E':
  225.             rlogin = escape = _POSIX_VDISABLE;
  226.             break;
  227.         case 'K':
  228. #ifdef    AUTHENTICATION
  229.             autologin = 0;
  230. #endif
  231.             break;
  232.         case 'L':
  233.             eight |= 2;    /* binary output only */
  234.             break;
  235.         case 'S':
  236.             {
  237. #ifdef    HAS_GETTOS
  238.             extern int tos;
  239.  
  240.             if ((tos = parsetos(optarg, "tcp")) < 0)
  241.                 fprintf(stderr, "%s%s%s%s\n",
  242.                     prompt, ": Bad TOS argument '",
  243.                     optarg,
  244.                     "; will try to use default TOS");
  245. #else
  246.             fprintf(stderr,
  247.                "%s: Warning: -S ignored, no parsetos() support.\n",
  248.                                 prompt);
  249. #endif
  250.             }
  251.             break;
  252.         case 'X':
  253. #ifdef    AUTHENTICATION
  254.             auth_disable_name(optarg);
  255. #endif
  256.             break;
  257.         case 'a':
  258.             autologin = 1;
  259.             break;
  260.         case 'c':
  261.             skiprc = 1;
  262.             break;
  263.         case 'd':
  264.             debug = 1;
  265.             break;
  266.         case 'e':
  267.             set_escape_char(optarg);
  268.             break;
  269.         case 'f':
  270. #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
  271.             if (forward_flags & OPTS_FORWARD_CREDS) {
  272.                 fprintf(stderr, 
  273.                     "%s: Only one of -f and -F allowed.\n",
  274.                     prompt);
  275.                 help (0);
  276.             }
  277.             forward_flags |= OPTS_FORWARD_CREDS;
  278. #else
  279.             fprintf(stderr,
  280.              "%s: Warning: -f ignored, no Kerberos V5 support.\n", 
  281.                 prompt);
  282. #endif
  283.             break;
  284.         case 'F':
  285. #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
  286.             if (forward_flags & OPTS_FORWARD_CREDS) {
  287.                 fprintf(stderr, 
  288.                     "%s: Only one of -f and -F allowed.\n",
  289.                     prompt);
  290.                 help (0);
  291.             }
  292.             forward_flags |= OPTS_FORWARD_CREDS;
  293.             forward_flags |= OPTS_FORWARDABLE_CREDS;
  294. #else
  295.             fprintf(stderr,
  296.              "%s: Warning: -F ignored, no Kerberos V5 support.\n", 
  297.                 prompt);
  298. #endif
  299.             break;
  300.         case 'k':
  301. #if defined(AUTHENTICATION) && defined(KRB4)
  302.             {
  303.             extern char *dest_realm, dst_realm_buf[], dst_realm_sz;
  304.             dest_realm = dst_realm_buf;
  305.             (void)strncpy(dest_realm, optarg, dst_realm_sz);
  306.             }
  307. #else
  308.             fprintf(stderr,
  309.                "%s: Warning: -k ignored, no Kerberos V4 support.\n",
  310.                                 prompt);
  311. #endif
  312.             break;
  313.         case 'l':
  314.             autologin = 1;
  315.             user = optarg;
  316.             break;
  317.         case 'n':
  318. #if defined(TN3270) && defined(unix)
  319.             /* distinguish between "-n oasynch" and "-noasynch" */
  320.             if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
  321.                 == 'n' && argv[optind - 1][2] == 'o') {
  322.                 if (!strcmp(optarg, "oasynch")) {
  323.                     noasynchtty = 1;
  324.                     noasynchnet = 1;
  325.                 } else if (!strcmp(optarg, "oasynchtty"))
  326.                     noasynchtty = 1;
  327.                 else if (!strcmp(optarg, "oasynchnet"))
  328.                     noasynchnet = 1;
  329.             } else
  330. #endif    /* defined(TN3270) && defined(unix) */
  331.                 SetNetTrace(optarg);
  332.             break;
  333.         case 'r':
  334.             rlogin = '~';
  335.             break;
  336.         case 't':
  337. #if defined(TN3270) && defined(unix)
  338.             transcom = tline;
  339.             (void)strcpy(transcom, optarg);
  340. #else
  341.             fprintf(stderr,
  342.                "%s: Warning: -t ignored, no TN3270 support.\n",
  343.                                 prompt);
  344. #endif
  345.             break;
  346.         case 'x':
  347. #ifdef    ENCRYPTION
  348.             encrypt_auto(1);
  349.             decrypt_auto(1);
  350. #else    /* ENCRYPTION */
  351.             fprintf(stderr,
  352.                 "%s: Warning: -x ignored, no ENCRYPT support.\n",
  353.                                 prompt);
  354. #endif    /* ENCRYPTION */
  355.             break;
  356.  
  357.         case '&':
  358.             help ();
  359.         case 'V':
  360.             puts (inetutils_version);
  361.             exit (0);
  362.  
  363.         case '?':
  364.             try_help ();
  365.  
  366.         default:
  367.             usage ();
  368.             /* NOTREACHED */
  369.         }
  370.     }
  371.     if (autologin == -1)
  372.         autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
  373.  
  374.     argc -= optind;
  375.     argv += optind;
  376.  
  377.     if (argc) {
  378.         char *args[7], **argp = args;
  379.  
  380.         if (argc > 2)
  381.             usage ();
  382.         *argp++ = prompt;
  383.         if (user) {
  384.             *argp++ = "-l";
  385.             *argp++ = user;
  386.         }
  387.         *argp++ = argv[0];        /* host */
  388.         if (argc > 1)
  389.             *argp++ = argv[1];    /* port */
  390.         *argp = 0;
  391.  
  392.         if (setjmp(toplevel) != 0)
  393.             Exit(0);
  394.         if (tn(argp - args, args) == 1)
  395.             return (0);
  396.         else
  397.             return (1);
  398.     }
  399.     (void)setjmp(toplevel);
  400.     for (;;) {
  401. #ifdef TN3270
  402.         if (shell_active)
  403.             shell_continue();
  404.         else
  405. #endif
  406.             command(1, 0, 0);
  407.     }
  408. }
  409.