check for error on socket()
[unix-history] / usr / src / usr.bin / uucp / uucico / cico.c
index c7036b3..db7fb33 100644 (file)
@@ -1,11 +1,10 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)cico.c     5.5 (Berkeley) %G%";
+static char sccsid[] = "@(#)cico.c     5.10 (Berkeley) %G%";
 #endif
 
 #endif
 
-#include "uucp.h"
 #include <signal.h>
 #include <signal.h>
+#include "uucp.h"
 #include <setjmp.h>
 #include <setjmp.h>
-#include <sys/types.h>
 #ifdef USG
 #include <termio.h>
 #endif
 #ifdef USG
 #include <termio.h>
 #endif
@@ -21,6 +20,14 @@ static char sccsid[] = "@(#)cico.c   5.5 (Berkeley) %G%";
 #include "uust.h"
 #include "uusub.h"
 
 #include "uust.h"
 #include "uusub.h"
 
+#if defined(VMS) && defined(BSDTCP)
+#define NOGETPEER
+#endif
+
+#ifdef BSD2_9
+#define NOGETPEER
+#endif
+
 jmp_buf Sjbuf;
 jmp_buf Pipebuf;
 
 jmp_buf Sjbuf;
 jmp_buf Pipebuf;
 
@@ -49,13 +56,14 @@ int Stattype[] = {
 };
 
 
 };
 
 
-int Errorrate = 0;
 int ReverseRole = 0;
 int StdErrIsTty = 0;
 int Role = SLAVE;
 int onesys = 0;
 int ReverseRole = 0;
 int StdErrIsTty = 0;
 int Role = SLAVE;
 int onesys = 0;
+int turntime = 30 * 60;        /* 30 minutes expressed in seconds */
 extern int LocalOnly;
 extern int LocalOnly;
-extern char MaxGrade;
+extern char MaxGrade, DefMaxGrade;
+extern char Myfullname[];
 
 #ifdef USG
 struct termio Savettyb;
 
 #ifdef USG
 struct termio Savettyb;
@@ -79,14 +87,13 @@ register char *argv[];
        register char *p;
        extern onintr(), timeout(), setdebug();
        extern char *pskip();
        register char *p;
        extern onintr(), timeout(), setdebug();
        extern char *pskip();
-       char rflags[30];
+       char rflags[MAXFULLNAME];
        char *ttyn;
        char *ttyn;
-#if defined(VMS) && defined(BSDTCP)
+#ifdef NOGETPEER
        u_long Hostnumber = 0;
        u_long Hostnumber = 0;
-#endif VMS && BSDTCP
+#endif NOGETPEER
 
        strcpy(Progname, "uucico");
 
        strcpy(Progname, "uucico");
-       uucpname(Myname);
 
        signal(SIGINT, onintr);
        signal(SIGHUP, onintr);
 
        signal(SIGINT, onintr);
        signal(SIGHUP, onintr);
@@ -96,12 +103,12 @@ register char *argv[];
        signal(SIGFPE, setdebug);
        ret = guinfo(getuid(), User, msg);
        strcpy(Loginuser, User);
        signal(SIGFPE, setdebug);
        ret = guinfo(getuid(), User, msg);
        strcpy(Loginuser, User);
+       uucpname(Myname);
        ASSERT(ret == 0, "BAD UID", CNULL, ret);
 
 #ifdef BSD4_2
        setlinebuf(stderr);
 #endif
        ASSERT(ret == 0, "BAD UID", CNULL, ret);
 
 #ifdef BSD4_2
        setlinebuf(stderr);
 #endif
-       rflags[0] = '\0';
        umask(WFMASK);
        strcpy(Rmtname, Myname);
        Ifn = Ofn = -1;
        umask(WFMASK);
        strcpy(Rmtname, Myname);
        Ifn = Ofn = -1;
@@ -111,7 +118,8 @@ register char *argv[];
                        Spool = &argv[1][2];
                        break;
                case 'g':
                        Spool = &argv[1][2];
                        break;
                case 'g':
-                       MaxGrade = argv[1][2];
+               case 'p':
+                       MaxGrade = DefMaxGrade = argv[1][2];
                        break;
                case 'r':
                        Role = atoi(&argv[1][2]);
                        break;
                case 'r':
                        Role = atoi(&argv[1][2]);
@@ -121,7 +129,8 @@ register char *argv[];
                        Role = MASTER;
                        break;
                case 's':
                        Role = MASTER;
                        break;
                case 's':
-                       sprintf(Rmtname, "%.7s", &argv[1][2]);
+                       strncpy(Rmtname, &argv[1][2], MAXBASENAME);
+                       Rmtname[MAXBASENAME] = '\0';
                        if (Rmtname[0] != '\0')
                                onesys = 1;
                        break;
                        if (Rmtname[0] != '\0')
                                onesys = 1;
                        break;
@@ -130,17 +139,19 @@ register char *argv[];
                        Debug = atoi(&argv[1][2]);
                        if (Debug <= 0)
                                Debug = 1;
                        Debug = atoi(&argv[1][2]);
                        if (Debug <= 0)
                                Debug = 1;
-                       strcat(rflags, argv[1]);
                        logent("ENABLED", "DEBUG");
                        break;
                        logent("ENABLED", "DEBUG");
                        break;
+               case 't':
+                       turntime = atoi(&argv[1][2])*60;/* minutes to seconds */
+                       break;
                case 'L':       /* local calls only */
                        LocalOnly++;
                        break;
                case 'L':       /* local calls only */
                        LocalOnly++;
                        break;
-#if defined(VMS) && defined(BSDTCP)
+#ifdef NOGETPEER
                case 'h':
                        Hostnumber = inet_addr(&argv[1][2]);
                        break;
                case 'h':
                        Hostnumber = inet_addr(&argv[1][2]);
                        break;
-#endif VMS && BSDTCP
+#endif NOGETPEER
                default:
                        printf("unknown flag %s (ignored)\n", argv[1]);
                        break;
                default:
                        printf("unknown flag %s (ignored)\n", argv[1]);
                        break;
@@ -153,7 +164,7 @@ register char *argv[];
                --argc; argv++;
        }
 
                --argc; argv++;
        }
 
-       /* Try to run as uucp -- rti!trt */
+       /* Try to run as uucp */
        setgid(getegid());
        setuid(geteuid());
 #ifdef TIOCNOTTY
        setgid(getegid());
        setuid(geteuid());
 #ifdef TIOCNOTTY
@@ -167,7 +178,7 @@ register char *argv[];
        }
 #endif TIOCNOTTY
 #ifdef BSD4_2
        }
 #endif TIOCNOTTY
 #ifdef BSD4_2
-       if (getpgrp(0) == 0) { /*We have no controlling terminal */
+       if (getpgrp(0) == 0) { /* We have no controlling terminal */
                setpgrp(0, getpid());
        }
 #endif BSD4_2
                setpgrp(0, getpid());
        }
 #endif BSD4_2
@@ -178,10 +189,9 @@ register char *argv[];
 
        if (Role == SLAVE) {
                /* check for /etc/nologin */
 
        if (Role == SLAVE) {
                /* check for /etc/nologin */
-               ultouch();      /* sets nologinflag as a side effect */
-               if (nologinflag) {
+               if (access(NOLOGIN, 0) == 0) {
                        logent(NOLOGIN, "UUCICO SHUTDOWN");
                        logent(NOLOGIN, "UUCICO SHUTDOWN");
-                       if (Debug)
+                       if (Debug > 4)
                                logent("DEBUGGING", "continuing anyway");
                        else
                                cleanup(1);
                                logent("DEBUGGING", "continuing anyway");
                        else
                                cleanup(1);
@@ -193,7 +203,8 @@ register char *argv[];
                if (isatty(0) ==  0) {
                        IsTcpIp = 1;
                        DEBUG(4, "TCPIP connection -- ioctl-s disabled\n", CNULL);
                if (isatty(0) ==  0) {
                        IsTcpIp = 1;
                        DEBUG(4, "TCPIP connection -- ioctl-s disabled\n", CNULL);
-               }
+               } else
+                       IsTcpIp = 0;
 #endif TCPIP
                /* initial handshake */
                onesys = 1;
 #endif TCPIP
                /* initial handshake */
                onesys = 1;
@@ -212,18 +223,19 @@ register char *argv[];
                Ifn = 0;
                Ofn = 1;
                fixmode(Ifn);
                Ifn = 0;
                Ofn = 1;
                fixmode(Ifn);
+               getbaud(Ifn);
                sprintf(file,"%s/%d", RMTDEBUG, getpid());
 #ifdef VMS
                /* hold the version number down */
                unlink(file);
                sprintf(file,"%s/%d", RMTDEBUG, getpid());
 #ifdef VMS
                /* hold the version number down */
                unlink(file);
-#endif
+#endif VMS
                freopen(file, "w", stderr);
 #ifdef BSD4_2
                setlinebuf(stderr);
 #else  !BSD4_2
                setbuf(stderr, NULL);
 #endif !BSD4_2
                freopen(file, "w", stderr);
 #ifdef BSD4_2
                setlinebuf(stderr);
 #else  !BSD4_2
                setbuf(stderr, NULL);
 #endif !BSD4_2
-               sprintf(msg, "here=%s", Myname);
+               sprintf(msg, "here=%s", Myfullname);
                omsg('S', msg, Ofn);
                signal(SIGALRM, timeout);
                alarm(MAXMSGTIME);
                omsg('S', msg, Ofn);
                signal(SIGALRM, timeout);
                alarm(MAXMSGTIME);
@@ -232,10 +244,9 @@ register char *argv[];
                        if (!IsTcpIp) {
 #ifdef USG
                                ret = ioctl(0, TCSETA, &Savettyb);
                        if (!IsTcpIp) {
 #ifdef USG
                                ret = ioctl(0, TCSETA, &Savettyb);
-#endif
-#ifndef        USG
+#else  !USG
                                ret = ioctl(0, TIOCSETP, &Savettyb);
                                ret = ioctl(0, TIOCSETP, &Savettyb);
-#endif
+#endif !USG
                        }
                        cleanup(0);
                }
                        }
                        cleanup(0);
                }
@@ -246,10 +257,9 @@ register char *argv[];
                                if (!IsTcpIp) {
 #ifdef USG
                                        ret = ioctl(0, TCSETA, &Savettyb);
                                if (!IsTcpIp) {
 #ifdef USG
                                        ret = ioctl(0, TCSETA, &Savettyb);
-#endif
-#ifndef        USG
+#else  !USG
                                        ret = ioctl(0, TIOCSETP, &Savettyb);
                                        ret = ioctl(0, TIOCSETP, &Savettyb);
-#endif
+#endif !USG
                                }
                                cleanup(0);
                        }
                                }
                                cleanup(0);
                        }
@@ -259,12 +269,22 @@ register char *argv[];
                alarm(0);
                q = &msg[1];
                p = pskip(q);
                alarm(0);
                q = &msg[1];
                p = pskip(q);
-               sprintf(Rmtname, "%.7s", q);
+               strncpy(Rmtname, q, MAXBASENAME);
+               Rmtname[MAXBASENAME] = '\0';
                sprintf(wkpre,"%s/%s", RMTDEBUG, Rmtname);
                unlink(wkpre);
                if (link(file, wkpre) == 0)
                        unlink(file);
                DEBUG(4, "sys-%s\n", Rmtname);
                sprintf(wkpre,"%s/%s", RMTDEBUG, Rmtname);
                unlink(wkpre);
                if (link(file, wkpre) == 0)
                        unlink(file);
                DEBUG(4, "sys-%s\n", Rmtname);
+               /* The versys will also do an alias on the incoming name */
+               if (versys(&Rmtname)) {
+                       /* If we don't know them, we won't talk to them... */
+#ifdef NOSTRANGERS
+                       logent(Rmtname, "UNKNOWN HOST");
+                       omsg('R', "You are unknown to me", Ofn);
+                       cleanup(0);
+#endif NOSTRANGERS
+               }
 #ifdef BSDTCP
                /* we must make sure they are really who they say they
                 * are. We compare the hostnumber with the number in the hosts
 #ifdef BSDTCP
                /* we must make sure they are really who they say they
                 * are. We compare the hostnumber with the number in the hosts
@@ -273,20 +293,21 @@ register char *argv[];
                if (IsTcpIp) {
                        struct hostent *hp;
                        char *cpnt, *inet_ntoa();
                if (IsTcpIp) {
                        struct hostent *hp;
                        char *cpnt, *inet_ntoa();
-                       int fromlen;
+                       int len;
                        struct sockaddr_in from;
                        struct sockaddr_in from;
+                       extern char PhoneNumber[];
 
 
-#ifndef        VMS
-                       fromlen = sizeof(from);
-                       if (getpeername(0, &from, &fromlen) < 0) {
+#ifdef NOGETPEER
+                       from.sin_addr.s_addr = Hostnumber;
+                       from.sin_family = AF_INET;
+#else  !NOGETPEER
+                       len = sizeof(from);
+                       if (getpeername(0, &from, &len) < 0) {
                                logent(Rmtname, "NOT A TCP CONNECTION");
                                omsg('R', "NOT TCP", Ofn);
                                cleanup(0);
                        }
                                logent(Rmtname, "NOT A TCP CONNECTION");
                                omsg('R', "NOT TCP", Ofn);
                                cleanup(0);
                        }
-#else  VMS
-                       from.sin_addr.s_addr = Hostnumber;
-                       from.sin_family = AF_INET;
-#endif VMS
+#endif !NOGETPEER
                        hp = gethostbyaddr(&from.sin_addr,
                                sizeof (struct in_addr), from.sin_family);
                        if (hp == 0) {
                        hp = gethostbyaddr(&from.sin_addr,
                                sizeof (struct in_addr), from.sin_family);
                        if (hp == 0) {
@@ -301,41 +322,35 @@ register char *argv[];
                        }
                        if (Debug>99)
                                logent(Rmtname,"Request from IP-Host name =");
                        }
                        if (Debug>99)
                                logent(Rmtname,"Request from IP-Host name =");
-                       /* The following is to determine if the name given us by
-                        * the Remote uucico matches any of the names(aliases)
+                       /*
+                        * The following is to determine if the name given us by
+                        * the Remote uucico matches any of the names
                         * given its network number (remote machine) in our
                         * host table.
                         * given its network number (remote machine) in our
                         * host table.
+                        * We could check the aliases, but that won't work in
+                        * all cases (like if you are running the domain
+                        * server, where you don't get any aliases). The only
+                        * reliable way I can think of that works in ALL cases
+                        * is too look up the site in L.sys and see if the
+                        * sitename matches what we would call him if we
+                        * originated the call.
                         */
                         */
-                       if (strncmp(q, hp->h_name, 7) == 0) {
+                       /* PhoneNumber contains the official network name of the                           host we are checking. (set in versys.c) */
+                       if (sncncmp(PhoneNumber, hp->h_name, SYSNSIZE) == 0) {
                                if (Debug > 99)
                                        logent(q,"Found in host Tables");
                                if (Debug > 99)
                                        logent(q,"Found in host Tables");
-                       } else { /* Scan The host aliases */
-                               for(alias=hp->h_aliases; *alias!=0 &&
-                                   strncmp(q, *alias, 7) != 0; ++alias)
-                                       ;
-                               if (strncmp(q, *alias, 7) != 0) {
-                                       logent(q, "FORGED HOSTNAME");
-                                       logent(inet_ntoa(from.sin_addr), "ORIGINATED AT");
-                                       omsg('R',"You're not who you claim to be");
-                                       cleanup(0);
-                               }
-#ifdef DEBUG
-                               if (Debug> 99)
-                                       logent(q,"Found in host Tables");
-#endif
+                       } else {
+                               logent(hp->h_name, "FORGED HOSTNAME");
+                               logent(inet_ntoa(from.sin_addr), "ORIGINATED AT");
+                               logent(PhoneNumber, "SHOULD BE");
+                               sprintf(wkpre, "You're not who you claim to be: %s !=  %s", hp->h_name, PhoneNumber);
+                               omsg('R', wkpre, Ofn);
+                               cleanup(0);
                        }
                }
 #endif BSDTCP
 
                        }
                }
 #endif BSDTCP
 
-#ifdef NOSTRANGERS
-               /* If we don't know them, we won't talk to them... */
-               if (versys(Rmtname)) {
-                       logent(Rmtname, "UNKNOWN HOST");
-                       omsg('R', "You are unknown to me", Ofn);
-                       cleanup(0);
-               }
-#endif NOSTRANGERS
-               if (mlock(Rmtname)) {
+               if (mlock(Rmtname) == FAIL) {
                        omsg('R', "LCK", Ofn);
                        cleanup(0);
                }
                        omsg('R', "LCK", Ofn);
                        cleanup(0);
                }
@@ -363,6 +378,17 @@ register char *argv[];
                        case 'Q':
                                seq = atoi(++p);
                                break;
                        case 'Q':
                                seq = atoi(++p);
                                break;
+                       case 'p':
+                               MaxGrade = DefMaxGrade = *++p;
+                               DEBUG(4, "MaxGrade set to %c\n", MaxGrade);
+                               break;
+                       case 'v':
+                               if (strncmp(p, "grade", 5) == 0) {
+                                       p += 6;
+                                       MaxGrade = DefMaxGrade = *p++;
+                                       DEBUG(4, "MaxGrade set to %c\n", MaxGrade);
+                               }
+                               break;
                        default:
                                break;
                        }
                        default:
                                break;
                        }
@@ -384,7 +410,7 @@ register char *argv[];
                else {
 #endif !GNXSEQ
                        systat(Rmtname, Stattype[7], Stattext[7]);
                else {
 #endif !GNXSEQ
                        systat(Rmtname, Stattype[7], Stattext[7]);
-                       logent("BAD SEQ", "HANDSHAKE FAIL");
+                       logent("BAD SEQ", "FAILED HANDSHAKE");
 #ifdef GNXSEQ
                        ulkseq();
 #endif GNXSEQ
 #ifdef GNXSEQ
                        ulkseq();
 #endif GNXSEQ
@@ -423,33 +449,28 @@ loop:
                setdebug(0);
                if (ret == FAIL)
                        cleanup(100);
                setdebug(0);
                if (ret == FAIL)
                        cleanup(100);
-               if (ret == 0)
+               if (ret == SUCCESS)
                        cleanup(0);
        } else if (Role == MASTER && callok(Rmtname) != 0) {
                logent("SYSTEM STATUS", "CAN NOT CALL");
                cleanup(0);
        }
 
                        cleanup(0);
        } else if (Role == MASTER && callok(Rmtname) != 0) {
                logent("SYSTEM STATUS", "CAN NOT CALL");
                cleanup(0);
        }
 
-       sprintf(wkpre, "%c.%.7s", CMDPRE, Rmtname);
+       sprintf(wkpre, "%c.%.*s", CMDPRE, SYSNSIZE, Rmtname);
 
        signal(SIGINT, SIG_IGN);
        signal(SIGQUIT, SIG_IGN);
        if (Role == MASTER) {
                /* check for /etc/nologin */
 
        signal(SIGINT, SIG_IGN);
        signal(SIGQUIT, SIG_IGN);
        if (Role == MASTER) {
                /* check for /etc/nologin */
-               ultouch();      /* sets nologinflag as a side effect */
-               if (nologinflag) {
+               if (access(NOLOGIN, 0) == 0) {
                        logent(NOLOGIN, "UUCICO SHUTDOWN");
                        logent(NOLOGIN, "UUCICO SHUTDOWN");
-                       if (Debug)
+                       if (Debug > 4)
                                logent("DEBUGGING", "continuing anyway");
                        else
                                cleanup(1);
                }
                /*  master part */
                signal(SIGHUP, SIG_IGN);
                                logent("DEBUGGING", "continuing anyway");
                        else
                                cleanup(1);
                }
                /*  master part */
                signal(SIGHUP, SIG_IGN);
-               if (!iswrk(file, "chk", Spool, wkpre) && !onesys) {
-                       logent(Rmtname, "NO WORK");
-                       goto next;
-               }
                if (Ifn != -1 && Role == MASTER) {
                        write(Ofn, EOTMSG, strlen(EOTMSG));
                        clsacu();
                if (Ifn != -1 && Role == MASTER) {
                        write(Ofn, EOTMSG, strlen(EOTMSG));
                        clsacu();
@@ -460,7 +481,7 @@ loop:
                        sleep(3);
                }
                sprintf(msg, "call to %s ", Rmtname);
                        sleep(3);
                }
                sprintf(msg, "call to %s ", Rmtname);
-               if (mlock(Rmtname) != 0) {
+               if (mlock(Rmtname) != SUCCESS) {
                        logent(msg, "LOCKED");
                        US_SST(us_s_lock);
                        goto next;
                        logent(msg, "LOCKED");
                        US_SST(us_s_lock);
                        goto next;
@@ -470,7 +491,7 @@ loop:
                        if (Ofn != CF_TIME)
                                logent(msg, _FAILED);
                        /* avoid excessive 'wrong time' info */
                        if (Ofn != CF_TIME)
                                logent(msg, _FAILED);
                        /* avoid excessive 'wrong time' info */
-                       if (Stattype[-Ofn] != SS_WRONGTIME || argv[0][0] != 'U'){
+                       if (Stattype[-Ofn] != SS_WRONGTIME){
                                systat(Rmtname, Stattype[-Ofn], Stattext[-Ofn]);
                                US_SST(-Ofn);
                                UB_SST(-Ofn);
                                systat(Rmtname, Stattype[-Ofn], Stattext[-Ofn]);
                                US_SST(-Ofn);
                                UB_SST(-Ofn);
@@ -488,7 +509,8 @@ loop:
                if (isatty(Ifn) ==  0) {
                        IsTcpIp = 1;
                        DEBUG(4, "TCPIP connection -- ioctl-s disabled\n", CNULL);
                if (isatty(Ifn) ==  0) {
                        IsTcpIp = 1;
                        DEBUG(4, "TCPIP connection -- ioctl-s disabled\n", CNULL);
-               }
+               } else
+                       IsTcpIp = 0;
 #endif
 
                if (setjmp(Sjbuf))
 #endif
 
                if (setjmp(Sjbuf))
@@ -511,7 +533,20 @@ loop:
 #else !GNXSEQ
                seq = 0;
 #endif !GNXSEQ
 #else !GNXSEQ
                seq = 0;
 #endif !GNXSEQ
-               sprintf(msg, "%.7s -Q%d %s", Myname, seq, rflags);
+               if (Debug)
+                       sprintf(rflags, "-x%d", Debug);
+               else
+                       rflags[0] = '\0';
+
+               if (MaxGrade != '\177') {
+                       char buf[MAXFULLNAME];
+                       sprintf(buf, " -p%c -vgrade=%c", MaxGrade, MaxGrade);
+                       strcat(rflags, buf);
+               }
+
+               sprintf(msg, "%s -Q%d %s", Myname, seq, rflags);
+               if (MaxGrade != '\177')
+                       DEBUG(2, "Max Grade this transfer is %c\n", MaxGrade);
                omsg('S', msg, Ofn);
                for (;;) {
                        ret = imsg(msg, Ifn);
                omsg('S', msg, Ofn);
                for (;;) {
                        ret = imsg(msg, Ifn);
@@ -530,7 +565,7 @@ loop:
                alarm(0);
                if (msg[1] == 'B') {
                        /* bad sequence */
                alarm(0);
                if (msg[1] == 'B') {
                        /* bad sequence */
-                       logent("BAD SEQ", "HANDSHAKE FAIL");
+                       logent("BAD SEQ", "FAILED HANDSHAKE");
                        US_SST(us_s_hand);
                        systat(Rmtname, SS_BADSEQ, Stattext[SS_BADSEQ]);
 #ifdef GNXSEQ
                        US_SST(us_s_hand);
                        systat(Rmtname, SS_BADSEQ, Stattext[SS_BADSEQ]);
 #ifdef GNXSEQ
@@ -539,14 +574,14 @@ loop:
                        goto next;
                }
                if (strcmp(&msg[1], "OK") != SAME)  {
                        goto next;
                }
                if (strcmp(&msg[1], "OK") != SAME)  {
-                       logent(&msg[1], "HANDSHAKE FAIL");
+                       logent(&msg[1], "FAILED HANDSHAKE");
                        US_SST(us_s_hand);
 #ifdef GNXSEQ
                        ulkseq();
 #endif GNXSEQ
                        systat(Rmtname, SS_INPROGRESS,
                                strcmp(&msg[1], "CB") == SAME?
                        US_SST(us_s_hand);
 #ifdef GNXSEQ
                        ulkseq();
 #endif GNXSEQ
                        systat(Rmtname, SS_INPROGRESS,
                                strcmp(&msg[1], "CB") == SAME?
-                               "AWAITING CALLBACK": "HANDSHAKE FAIL");
+                               "AWAITING CALLBACK": "FAILED HANDSHAKE");
                        goto next;
                }
 #ifdef GNXSEQ
                        goto next;
                }
 #ifdef GNXSEQ
@@ -559,7 +594,7 @@ loop:
        DEBUG(1, "Loginuser - %s\n", Loginuser);
 
        alarm(MAXMSGTIME);
        DEBUG(1, "Loginuser - %s\n", Loginuser);
 
        alarm(MAXMSGTIME);
-       if (setjmp(Sjbuf))
+       if (ret=setjmp(Sjbuf))
                goto Failure;
        ret = startup(Role);
        alarm(0);
                goto Failure;
        ret = startup(Role);
        alarm(0);
@@ -567,7 +602,8 @@ loop:
                logent("startup", _FAILED);
 Failure:
                US_SST(us_s_start);
                logent("startup", _FAILED);
 Failure:
                US_SST(us_s_start);
-               systat(Rmtname, SS_FAIL, "STARTUP FAILED");
+               systat(Rmtname, SS_FAIL, ret > 0 ? "CONVERSATION FAILED" :
+                       "STARTUP FAILED");
                goto next;
        } else {
                logent("startup", "OK");
                goto next;
        } else {
                logent("startup", "OK");
@@ -623,7 +659,6 @@ struct sgttyb Hupvec;
 cleanup(code)
 register int code;
 {
 cleanup(code)
 register int code;
 {
-       register int ret;
        register char *ttyn;
        char bfr[BUFSIZ];
        struct stat sbuf;
        register char *ttyn;
        char bfr[BUFSIZ];
        struct stat sbuf;
@@ -637,19 +672,24 @@ register int code;
                if (!IsTcpIp) {
 #ifdef USG
                        Savettyb.c_cflag |= HUPCL;
                if (!IsTcpIp) {
 #ifdef USG
                        Savettyb.c_cflag |= HUPCL;
-                       ret = ioctl(0, TCSETA, &Savettyb);
+                       (void) ioctl(0, TCSETA, &Savettyb);
 #else !USG
 #else !USG
-                       ret = ioctl(0, TIOCHPCL, STBNULL);
-                       ret = ioctl(0, TIOCGETP, &Hupvec);
+                       (void) ioctl(0, TIOCHPCL, STBNULL);
+#ifdef TIOCSDTR
+                       (void) ioctl(0, TIOCCDTR, STBNULL);
+                       sleep(2);
+                       (void) ioctl(0, TIOCSDTR, STBNULL);
+#else !TIOCSDTR
+                       (void) ioctl(0, TIOCGETP, &Hupvec);
+#endif !TIOCSDTR
                        Hupvec.sg_ispeed = B0;
                        Hupvec.sg_ospeed = B0;
                        Hupvec.sg_ispeed = B0;
                        Hupvec.sg_ospeed = B0;
-                       ret = ioctl(0, TIOCSETP, &Hupvec);
+                       (void) ioctl(0, TIOCSETP, &Hupvec);
                        sleep(2);
                        sleep(2);
-                       ret = ioctl(0, TIOCSETP, &Savettyb);
+                       (void) ioctl(0, TIOCSETP, &Savettyb);
                        /* make *sure* exclusive access is off */
                        /* make *sure* exclusive access is off */
-                       ret = ioctl(0, TIOCNXCL, STBNULL);
+                       (void) ioctl(0, TIOCNXCL, STBNULL);
 #endif !USG
 #endif !USG
-                       DEBUG(4, "ret ioctl - %d\n", ret);
                }
                ttyn = ttyname(Ifn);
                if (ttyn != NULL)
                }
                ttyn = ttyname(Ifn);
                if (ttyn != NULL)
@@ -661,6 +701,10 @@ register int code;
                close(Ifn);
                close(Ofn);
        }
                close(Ifn);
                close(Ofn);
        }
+#ifdef DIALINOUT
+       /* reenable logins on dialout */
+       reenable();
+#endif DIALINOUT
        if (code == 0)
                xuuxqt();
        else
        if (code == 0)
                xuuxqt();
        else
@@ -685,7 +729,7 @@ register int inter;
        sprintf(str, "SIGNAL %d", inter);
        logent(str, "CAUGHT");
        US_SST(us_s_intr);
        sprintf(str, "SIGNAL %d", inter);
        logent(str, "CAUGHT");
        US_SST(us_s_intr);
-       if (*Rmtname && strcmp(Rmtname, Myname))
+       if (*Rmtname && strncmp(Rmtname, Myname, MAXBASENAME))
                systat(Rmtname, SS_FAIL, str);
        if (inter == SIGPIPE && !onesys)
                longjmp(Pipebuf, 1);
                systat(Rmtname, SS_FAIL, str);
        if (inter == SIGPIPE && !onesys)
                longjmp(Pipebuf, 1);
@@ -702,7 +746,13 @@ int code;
 {
        char buf[BUFSIZ];
 
 {
        char buf[BUFSIZ];
 
-       if (!StdErrIsTty) {
+       if (code) {
+               if (Debug == 0)
+                       Debug = 30;
+               else
+                       Debug = 0;
+       }
+       if (Debug && !StdErrIsTty) {
                sprintf(buf,"%s/%s", RMTDEBUG, Rmtname);
                unlink(buf);
                freopen(buf, "w", stderr);
                sprintf(buf,"%s/%s", RMTDEBUG, Rmtname);
                unlink(buf);
                freopen(buf, "w", stderr);
@@ -712,12 +762,6 @@ int code;
                setbuf(stderr, NULL);
 #endif !BSD4_2
        }
                setbuf(stderr, NULL);
 #endif !BSD4_2
        }
-       if (code) {
-               if (Debug == 0)
-                       Debug = 30;
-               else
-                       Debug = 0;
-       }
 }
 
 
 }
 
 
@@ -736,41 +780,42 @@ register int tty;
 #ifndef        USG
        struct sgttyb ttbuf;
 #endif
 #ifndef        USG
        struct sgttyb ttbuf;
 #endif
-       register int ret;
 
        if (IsTcpIp)
                return;
 #ifdef USG
 
        if (IsTcpIp)
                return;
 #ifdef USG
-       ret = ioctl(tty, TCGETA, &ttbuf);
+       ioctl(tty, TCGETA, &ttbuf);
        ttbuf.c_iflag = ttbuf.c_oflag = ttbuf.c_lflag = (ushort)0;
        ttbuf.c_cflag &= (CBAUD);
        ttbuf.c_cflag |= (CS8|CREAD);
        ttbuf.c_cc[VMIN] = 6;
        ttbuf.c_cc[VTIME] = 1;
        ttbuf.c_iflag = ttbuf.c_oflag = ttbuf.c_lflag = (ushort)0;
        ttbuf.c_cflag &= (CBAUD);
        ttbuf.c_cflag |= (CS8|CREAD);
        ttbuf.c_cc[VMIN] = 6;
        ttbuf.c_cc[VTIME] = 1;
-       ret = ioctl(tty, TCSETA, &ttbuf);
+       ioctl(tty, TCSETA, &ttbuf);
 #endif
 #ifndef        USG
        ioctl(tty, TIOCGETP, &ttbuf);
        ttbuf.sg_flags = (ANYP | RAW);
 #endif
 #ifndef        USG
        ioctl(tty, TIOCGETP, &ttbuf);
        ttbuf.sg_flags = (ANYP | RAW);
-       ret = ioctl(tty, TIOCSETP, &ttbuf);
+       ioctl(tty, TIOCSETP, &ttbuf);
 #endif
 #endif
-/*     ASSERT(ret >= 0, "STTY FAILED", CNULL, ret); */
 #ifndef        USG
 #ifndef        USG
-       ret = ioctl(tty, TIOCEXCL, STBNULL);
+       ioctl(tty, TIOCEXCL, STBNULL);
 #endif
 }
 
 
 #endif
 }
 
 
-/***
+/*
  *     timeout()       catch SIGALRM routine
  */
 
 timeout()
 {
  *     timeout()       catch SIGALRM routine
  */
 
 timeout()
 {
-       logent(Rmtname, "TIMEOUT");
-       if (*Rmtname && strcmp(Rmtname, Myname)) {
-               US_SST(us_s_tmot);
-               systat(Rmtname, SS_FAIL, "TIMEOUT");
+       extern int HaveSentHup;
+       if (!HaveSentHup) {
+               logent(Rmtname, "TIMEOUT");
+               if (*Rmtname && strncmp(Rmtname, Myname, MAXBASENAME)) {
+                       US_SST(us_s_tmot);
+                       systat(Rmtname, SS_FAIL, "TIMEOUT");
+               }
        }
        longjmp(Sjbuf, 1);
 }
        }
        longjmp(Sjbuf, 1);
 }
@@ -781,7 +826,7 @@ register char *p;
 {
        while(*p && *p != ' ')
                ++p;
 {
        while(*p && *p != ' ')
                ++p;
-       if(*p)
+       while(*p && *p == ' ')
                *p++ = 0;
        return p;
 }
                *p++ = 0;
        return p;
 }