diff options
Diffstat (limited to 'attacher.c')
-rw-r--r-- | attacher.c | 222 |
1 files changed, 102 insertions, 120 deletions
@@ -26,6 +26,7 @@ RCS_ID("$Id: attacher.c,v 1.8 1994/05/31 12:31:32 mlschroe Exp $ FAU") #include <sys/types.h> #include <sys/stat.h> +#include <sys/ioctl.h> #include <fcntl.h> #include <signal.h> #include "config.h" @@ -35,9 +36,6 @@ RCS_ID("$Id: attacher.c,v 1.8 1994/05/31 12:31:32 mlschroe Exp $ FAU") #include <pwd.h> static sigret_t AttacherSigInt __P(SIGPROTOARG); -#ifdef PASSWORD -static void trysend __P((int, struct msg *, char *)); -#endif #if defined(SIGWINCH) && defined(TIOCGWINSZ) static sigret_t AttacherWinch __P(SIGPROTOARG); #endif @@ -50,15 +48,17 @@ static void screen_builtin_lck __P((void)); #ifdef DEBUG static sigret_t AttacherChld __P(SIGPROTOARG); #endif +#ifdef MULTIUSER +static sigret_t AttachSigCont __P(SIGPROTOARG); +#endif extern int real_uid, real_gid, eff_uid, eff_gid; extern char *SockName, *SockMatch, SockPath[]; extern struct passwd *ppp; -extern char *attach_tty, *attach_term, *LoginName; +extern char *attach_tty, *attach_term, *LoginName, *preselect; extern int xflag, dflag, rflag, quietflag, adaptflag; extern struct mode attach_Mode; extern int MasterPid; -extern int nethackflag; #ifdef MULTIUSER extern char *multi; @@ -70,6 +70,18 @@ static int multipipe[2]; #endif +#ifdef MULTIUSER +static int ContinuePlease; + +static sigret_t +AttachSigCont SIGDEFARG +{ + debug("SigCont()\n"); + ContinuePlease = 1; + SIGRETURN; +} +#endif + /* * Send message to a screen backend. @@ -164,6 +176,7 @@ int how; bzero((char *) &m, sizeof(m)); m.type = how; + m.protocol_revision = MSG_REVISION; strncpy(m.m_tty, attach_tty, sizeof(m.m_tty) - 1); m.m_tty[sizeof(m.m_tty) - 1] = 0; @@ -191,7 +204,7 @@ int how; switch (n) { case 0: - if (rflag == 2) + if (rflag && (rflag & 1) == 0) return 0; if (quietflag) eexit(10); @@ -203,9 +216,12 @@ int how; case 1: break; default: - if (quietflag) - eexit(10 + n); - Panic(0, "Type \"screen [-d] -r [pid.]tty.host\" to resume one of them."); + if (rflag < 3) + { + if (quietflag) + eexit(10 + n); + Panic(0, "Type \"screen [-d] -r [pid.]tty.host\" to resume one of them."); + } /* NOTREACHED */ } } @@ -220,7 +236,10 @@ int how; setuid(real_uid); #if defined(MULTIUSER) && defined(USE_SETEUID) else - xseteuid(real_uid); /* multi_uid, allow backend to send signals */ + { + /* This call to xsetuid should also set the saved uid */ + xseteuid(real_uid); /* multi_uid, allow backend to send signals */ + } #endif setgid(real_gid); eff_uid = real_uid; @@ -240,6 +259,17 @@ int how; Panic(errno, "stat %s", SockPath); if ((st.st_mode & 0600) != 0600) Panic(0, "Socket is in wrong mode (%03o)", (int)st.st_mode); + + /* + * Change: if -x or -r ignore failing -d + */ + if ((xflag || rflag) && dflag && (st.st_mode & 0700) == 0600) + dflag = 0; + + /* + * Without -x, the mode must match. + * With -x the mode is irrelevant unless -d. + */ if ((dflag || !xflag) && (st.st_mode & 0700) != (dflag ? 0700 : 0600)) Panic(0, "That screen is %sdetached.", dflag ? "already " : "not "); #ifdef REMOTE_DETACH @@ -273,6 +303,10 @@ int how; strncpy(m.m.attach.auser, LoginName, sizeof(m.m.attach.auser) - 1); m.m.attach.auser[sizeof(m.m.attach.auser) - 1] = 0; + m.m.attach.esc = DefaultEsc; + m.m.attach.meta_esc = DefaultMetaEsc; + strncpy(m.m.attach.preselect, preselect ? preselect : "", sizeof(m.m.attach.preselect) - 1); + m.m.attach.preselect[sizeof(m.m.attach.preselect) - 1] = 0; m.m.attach.apid = getpid(); m.m.attach.adaptflag = adaptflag; m.m.attach.lines = m.m.attach.columns = 0; @@ -281,23 +315,23 @@ int how; if ((s = getenv("COLUMNS"))) m.m.attach.columns = atoi(s); -#ifdef PASSWORD - if (how == MSG_ATTACH || how == MSG_CONT) - trysend(lasts, &m, m.m.attach.password); - else +#ifdef MULTIUSER + /* setup CONT signal handler to repair the terminal mode */ + if (multi && (how == MSG_ATTACH || how == MSG_CONT)) + signal(SIGCONT, AttachSigCont); #endif - { - if (write(lasts, (char *) &m, sizeof(m)) != sizeof(m)) - Panic(errno, "write"); - close(lasts); - } + + if (write(lasts, (char *) &m, sizeof(m)) != sizeof(m)) + Panic(errno, "write"); + close(lasts); debug1("Attach(%d): sent\n", m.type); #ifdef MULTIUSER if (multi && (how == MSG_ATTACH || how == MSG_CONT)) { -# ifndef PASSWORD - pause(); -# endif + while (!ContinuePlease) + pause(); /* wait for SIGCONT */ + signal(SIGCONT, SIG_DFL); + ContinuePlease = 0; # ifndef USE_SETEUID close(multipipe[1]); # else @@ -315,77 +349,11 @@ int how; } -#ifdef PASSWORD - -static trysendstatok, trysendstatfail; - -static sigret_t -trysendok SIGDEFARG -{ - trysendstatok = 1; -} - -static sigret_t -trysendfail SIGDEFARG -{ -# ifdef SYSVSIGS - signal(SIG_PW_FAIL, trysendfail); -# endif /* SYSVSIGS */ - trysendstatfail = 1; -} - -static char screenpw[9]; - -static void -trysend(fd, m, pwto) -int fd; -struct msg *m; -char *pwto; -{ - char *npw = NULL; - sigret_t (*sighup)__P(SIGPROTOARG); - sigret_t (*sigusr1)__P(SIGPROTOARG); - int tries; - - sigusr1 = signal(SIG_PW_OK, trysendok); - sighup = signal(SIG_PW_FAIL, trysendfail); - for (tries = 0; ; ) - { - strncpy(pwto, screenpw, 9); - trysendstatok = trysendstatfail = 0; - if (write(fd, (char *) m, sizeof(*m)) != sizeof(*m)) - Panic(errno, "write"); - close(fd); - while (trysendstatok == 0 && trysendstatfail == 0) - pause(); - if (trysendstatok) - { - signal(SIG_PW_OK, sigusr1); - signal(SIG_PW_FAIL, sighup); - if (trysendstatfail) - kill(getpid(), SIG_PW_FAIL); - return; - } - if (++tries > 1 || (npw = getpass("Screen Password:")) == 0 || *npw == 0) - { -#ifdef NETHACK - if (nethackflag) - Panic(0, "The guard slams the door in your face."); - else +#if defined(DEBUG) || !defined(DO_NOT_POLL_MASTER) +static int AttacherPanic; #endif - Panic(0, "Password incorrect."); - } - strncpy(screenpw, npw, 8); - if ((fd = MakeClientSocket(0)) == -1) - Panic(0, "Cannot contact screen again. Sigh."); - } -} -#endif /* PASSWORD */ - #ifdef DEBUG -static int AttacherPanic; - static sigret_t AttacherChld SIGDEFARG { @@ -394,6 +362,17 @@ AttacherChld SIGDEFARG } #endif +static sigret_t +AttacherSigAlarm SIGDEFARG +{ +#ifdef DEBUG + static int tick_cnt = 0; + if ((tick_cnt = (tick_cnt + 1) % 4) == 0) + debug("tick\n"); +#endif + SIGRETURN; +} + /* * the frontend's Interrupt handler * we forward SIGINT to the poor backend @@ -430,6 +409,7 @@ AttacherFinit SIGDEFARG debug1("attach_tty is %s\n", attach_tty); m.m.detach.dpid = getpid(); m.type = MSG_HANGUP; + m.protocol_revision = MSG_REVISION; if ((s = MakeClientSocket(0)) >= 0) { write(s, (char *)&m, sizeof(m)); @@ -471,6 +451,23 @@ AttacherFinitBye SIGDEFARG } #endif +#if defined(DEBUG) && defined(SIG_NODEBUG) +static sigret_t +AttacherNoDebug SIGDEFARG +{ + debug("AttacherNoDebug()\n"); + signal(SIG_NODEBUG, AttacherNoDebug); + if (dfp) + { + debug("debug: closing debug file.\n"); + fflush(dfp); + fclose(dfp); + dfp = NULL; + } + SIGRETURN; +} +#endif /* SIG_NODEBUG */ + static int SuspendPlease; static sigret_t @@ -521,6 +518,9 @@ Attacher() #ifdef POW_DETACH signal(SIG_POWER_BYE, AttacherFinitBye); #endif +#if defined(DEBUG) && defined(SIG_NODEBUG) + signal(SIG_NODEBUG, AttacherNoDebug); +#endif #ifdef LOCK signal(SIG_LOCK, DoLock); #endif @@ -541,8 +541,11 @@ Attacher() #endif for (;;) { -#ifdef DEBUG - sleep(1); +#ifndef DO_NOT_POLL_MASTER + signal(SIGALRM, AttacherSigAlarm); + alarm(15); + pause(); + alarm(0); if (kill(MasterPid, 0) < 0 && errno != EPERM) { debug1("attacher: Panic! MasterPid %d does not exist.\n", MasterPid); @@ -551,16 +554,17 @@ Attacher() #else pause(); #endif -/* - debug("attacher: ding!\n"); -*/ -#ifdef DEBUG +#if defined(DEBUG) || !defined(DO_NOT_POLL_MASTER) if (AttacherPanic) { +# ifdef FORKDEBUG + exit(0); +# else fcntl(0, F_SETFL, 0); SetTTY(0, &attach_Mode); printf("\nSuddenly the Dungeon collapses!! - You die...\n"); eexit(1); +# endif } #endif #ifdef BSDJOBS @@ -660,14 +664,7 @@ LockTerminal() exit(errno); } if (pid == -1) - { -#ifdef NETHACK - if (nethackflag) - Msg(errno, "Cannot fork terminal - lock failed"); - else -#endif - Msg(errno, "Cannot lock terminal - fork failed"); - } + Msg(errno, "Cannot lock terminal - fork failed"); else { #ifdef BSDWAIT @@ -732,21 +729,6 @@ screen_builtin_lck() char fullname[100], *cp1, message[100 + 100]; char *pass, mypass[9]; -#ifdef undef - /* get password entry */ - if ((ppp = getpwuid(real_uid)) == NULL) - { - fprintf(stderr, "screen_builtin_lck: No passwd entry.\007\n"); - sleep(2); - return; - } - if (!isatty(0)) - { - fprintf(stderr, "screen_builtin_lck: Not a tty.\007\n"); - sleep(2); - return; - } -#endif pass = ppp->pw_passwd; if (pass == 0 || *pass == 0) { @@ -778,6 +760,7 @@ screen_builtin_lck() debug("screen_builtin_lck looking in gcos field\n"); strncpy(fullname, ppp->pw_gecos, sizeof(fullname) - 9); fullname[sizeof(fullname) - 9] = 0; + if ((cp1 = index(fullname, ',')) != NULL) *cp1 = '\0'; if ((cp1 = index(fullname, '&')) != NULL) @@ -801,7 +784,6 @@ screen_builtin_lck() AttacherFinit(SIGARG); /* NOTREACHED */ } - debug3("getpass(%d): %x == %s\n", errno, (unsigned int)cp1, cp1); if (pass) { if (!strncmp(crypt(cp1, pass), pass, strlen(pass))) |