change USER-passing to happen only if -a or -l user specified
authorMike Karels <karels@ucbvax.Berkeley.EDU>
Sat, 28 Jul 1990 14:28:38 +0000 (06:28 -0800)
committerMike Karels <karels@ucbvax.Berkeley.EDU>
Sat, 28 Jul 1990 14:28:38 +0000 (06:28 -0800)
(still not quite right, "open -l name" defines USER=name and exports);
use current user id if different than login name; getopting and reformatting
of main.c by bostic

SCCS-vsn: usr.bin/telnet/main.c 1.15
SCCS-vsn: usr.bin/telnet/commands.c 1.31

usr/src/usr.bin/telnet/commands.c
usr/src/usr.bin/telnet/main.c

index 097edce..71d87f8 100644 (file)
@@ -6,7 +6,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)commands.c 1.30 (Berkeley) %G%";
+static char sccsid[] = "@(#)commands.c 1.31 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -22,6 +22,7 @@ static char sccsid[] = "@(#)commands.c        1.30 (Berkeley) %G%";
 #include <signal.h>
 #include <netdb.h>
 #include <ctype.h>
 #include <signal.h>
 #include <netdb.h>
 #include <ctype.h>
+#include <pwd.h>
 #include <varargs.h>
 
 #include <arpa/telnet.h>
 #include <varargs.h>
 
 #include <arpa/telnet.h>
@@ -1609,6 +1610,7 @@ env_init()
                free(ep->value);
                ep->value = cp;
        }
                free(ep->value);
                ep->value = cp;
        }
+#ifdef notdef
        /*
         * If USER is not defined, but LOGNAME is, then add
         * USER with the value from LOGNAME.
        /*
         * If USER is not defined, but LOGNAME is, then add
         * USER with the value from LOGNAME.
@@ -1616,6 +1618,7 @@ env_init()
        if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME")))
                env_define("USER", ep->value);
        env_export("USER");
        if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME")))
                env_define("USER", ep->value);
        env_export("USER");
+#endif
        env_export("DISPLAY");
        env_export("PRINTER");
 }
        env_export("DISPLAY");
        env_export("PRINTER");
 }
@@ -1920,6 +1923,8 @@ char *name, *proto;
 }
 #endif
 
 }
 #endif
 
+extern int autologin;
+
 int
 tn(argc, argv)
        int argc;
 int
 tn(argc, argv)
        int argc;
@@ -2097,8 +2102,22 @@ tn(argc, argv)
        connected++;
     } while (connected == 0);
     cmdrc(hostp, hostname);
        connected++;
     } while (connected == 0);
     cmdrc(hostp, hostname);
-    if (user)
+    if (autologin && user == NULL) {
+       struct passwd *pw;
+       uid_t uid = getuid();
+
+       user = getlogin();
+       if (user == NULL ||
+           (pw = getpwnam(user)) && pw->pw_uid != uid)
+               if (pw = getpwuid(uid))
+                       user = pw->pw_name;
+               else
+                       user = NULL;
+    }
+    if (user) {
        env_define("USER", user);
        env_define("USER", user);
+       env_export("USER");
+    }
     (void) call(status, "status", "notmuch", 0);
     if (setjmp(peerdied) == 0)
        telnet();
     (void) call(status, "status", "notmuch", 0);
     if (setjmp(peerdied) == 0)
        telnet();
index a159c3e..12ebeda 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 1988 Regents of the University of California.
+ * Copyright (c) 1988, 1990 Regents of the University of California.
  * All rights reserved.
  *
  * %sccs.include.redist.c%
  * All rights reserved.
  *
  * %sccs.include.redist.c%
 
 #ifndef lint
 char copyright[] =
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1988 Regents of the University of California.\n\
+"@(#) Copyright (c) 1988, 1990 Regents of the University of California.\n\
  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)main.c     1.14 (Berkeley) %G%";
+static char sccsid[] = "@(#)main.c     1.15 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
+#include <string.h>
 
 #include "ring.h"
 
 #include "ring.h"
-
 #include "externs.h"
 #include "defines.h"
 
 /*
  * Initialize variables.
  */
 #include "externs.h"
 #include "defines.h"
 
 /*
  * Initialize variables.
  */
-
 void
 tninit()
 {
 void
 tninit()
 {
-    init_terminal();
-
-    init_network();
-    
-    init_telnet();
-
-    init_sys();
-
-    init_3270();
+       init_terminal();
+       init_network();
+       init_telnet();
+       init_sys();
+       init_3270();
 }
 
 }
 
+int    autologin;
 
 /*
  * main.  Parse arguments, invoke the protocol or command parser.
  */
 
 /*
  * main.  Parse arguments, invoke the protocol or command parser.
  */
-
-
-int
 main(argc, argv)
        int argc;
        char *argv[];
 {
 main(argc, argv)
        int argc;
        char *argv[];
 {
-    char *user = 0;
-
-    tninit();          /* Clear out things */
-#ifdef CRAY
-    _setlist_init();   /* Work around compiler bug */
+       extern char *optarg;
+       extern int optind;
+       int ch;
+       char *user;
+
+       tninit();               /* Clear out things */
+#ifdef CRAY
+       _setlist_init();        /* Work around compiler bug */
 #endif
 #endif
-
-    TerminalSaveState();
-
-    prompt = argv[0];
-    while ((argc > 1) && (argv[1][0] == '-')) {
-       if (!strcmp(argv[1], "-d")) {
-           debug = 1;
-       } else if (!strcmp(argv[1], "-n")) {
-           if ((argc > 1) && (argv[2][0] != '-')) {    /* get file name */
-               SetNetTrace(argv[2]);
-               argv++;
-               argc--;
-           }
-       } else if (!strcmp(argv[1], "-l")) {
-           if ((argc > 1) && (argv[2][0] != '-')) {    /* get user name */
-               user = argv[2];
-               argv++;
-               argc--;
-           }
-       } else if (!strncmp(argv[1], "-e", 2)) {
-               set_escape_char(&argv[1][2]);
-       } else {
-#if    defined(TN3270) && defined(unix)
-           if (!strcmp(argv[1], "-t")) {
-               if ((argc > 1) && (argv[2][0] != '-')) { /* get file name */
-                   transcom = tline;
-                   (void) strcpy(transcom, argv[2]);
-                   argv++;
-                   argc--;
-               }
-           } else if (!strcmp(argv[1], "-noasynch")) {
-               noasynchtty = 1;
-               noasynchnet = 1;
-           } else if (!strcmp(argv[1], "-noasynchtty")) {
-               noasynchtty = 1;
-           } else if (!strcmp(argv[1], "-noasynchnet")) {
-               noasynchnet = 1;
-           } else
+       TerminalSaveState();
+
+       if (prompt = rindex(argv[0], '/'))
+               ++prompt;
+       else
+               prompt = argv[0];
+
+       user = NULL;
+       autologin = 0;
+       while ((ch = getopt(argc, argv, "ade:l:n:")) != EOF)
+               switch(ch) {
+               case 'a':
+                       autologin = 1;
+                       break;
+               case 'd':
+                       debug = 1;
+                       break;
+               case 'e':
+                       set_escape_char(optarg);
+                       break;
+               case 'l':
+                       autologin = 1;
+                       user = optarg;
+                       break;
+               case 'n':
+#if defined(TN3270) && defined(unix)
+                       /* distinguish between "-n oasynch" and "-noasynch" */
+                       if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
+                           == 'n' && argv[optind - 1][2] == 'o') {
+                               if (!strcmp(optarg, "oasynch")) {
+                                       noasynchtty = 1;
+                                       noasynchnet = 1;
+                               } else if (!strcmp(optarg, "oasynchtty"))
+                                       noasynchtty = 1;
+                               } else if (!strcmp(optarg, "oasynchnet"))
+                                       noasynchnet = 1;
+                               }
+                       } else
 #endif /* defined(TN3270) && defined(unix) */
 #endif /* defined(TN3270) && defined(unix) */
-           if (argv[1][1] != '\0') {
-               fprintf(stderr, "Unknown option *%s*.\n", argv[1]);
-           }
-       }
-       argc--;
-       argv++;
-    }
-    if (argc != 1) {
-       if (setjmp(toplevel) != 0)
-           Exit(0);
-       if (user) {
-           argc += 2;
-           argv -= 2;
-           argv[0] = argv[2];
-           argv[1] = "-l";
-           argv[2] = user;
-       }
-       if (tn(argc, argv) == 1) {
-           return 0;
-       } else {
-           return 1;
-       }
-    }
-    (void) setjmp(toplevel);
-    for (;;) {
-#if    !defined(TN3270)
-       command(1, 0, 0);
-#else  /* !defined(TN3270) */
-       if (!shell_active) {
-           command(1, 0, 0);
-       } else {
-#if    defined(TN3270)
-           shell_continue();
-#endif /* defined(TN3270) */
+                               SetNetTrace(optarg);
+                       break;
+#if defined(TN3270) && defined(unix)
+               case 't':
+                       transcom = tline;
+                       (void)strcpy(transcom, optarg);
+                       break;
+#endif
+               case '?':
+               default:
+                       usage();
+                       /* NOTREACHED */
+               }
+       argc -= optind;
+       argv += optind;
+
+       if (argc) {
+               char *args[7], **argp = args;
+
+               if (argc > 2)
+                       usage();
+               *argp++ = prompt;
+               if (user) {
+                       *argp++ = "-l";
+                       *argp++ = user;
+               }
+               *argp++ = argv[0];              /* host */
+               if (argc > 1)
+                       *argp++ = argv[1];      /* port */
+               *argp = 0;
+
+               if (setjmp(toplevel) != 0)
+                       Exit(0);
+               if (tn(argp - args, args) == 1)
+                       return (0);
+               else
+                       return (1);
        }
        }
-#endif /* !defined(TN3270) */
-    }
+       (void)setjmp(toplevel);
+       for (;;)
+#ifdef TN3270
+               if (shell_active)
+                       shell_continue();
+               else
+#endif
+                       command(1, 0, 0);
+}
+
+usage()
+{
+       fprintf(stderr, "usage: %s [-a] [ [-l user] host-name [port] ]\n",
+           prompt);
+       exit(1);
 }
 }