1 /* Copyright (c) 2008, 2009
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Micah Cowan (micah@cowan.name)
5 * Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
6 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
7 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
8 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
9 * Copyright (c) 1987 Oliver Laumann
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program (see the file COPYING); if not, see
23 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 ****************************************************************
29 #include <sys/types.h>
31 #include <sys/ioctl.h>
40 static int WriteMessage
__P((int, struct msg
*));
41 static sigret_t AttacherSigInt
__P(SIGPROTOARG
);
42 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
43 static sigret_t AttacherWinch
__P(SIGPROTOARG
);
46 static sigret_t DoLock
__P(SIGPROTOARG
);
47 static void LockTerminal
__P((void));
48 static sigret_t LockHup
__P(SIGPROTOARG
);
49 static void screen_builtin_lck
__P((void));
52 static sigret_t AttacherChld
__P(SIGPROTOARG
);
54 static sigret_t AttachSigCont
__P(SIGPROTOARG
);
56 extern int real_uid
, real_gid
, eff_uid
, eff_gid
;
57 extern char *SockName
, *SockMatch
, SockPath
[];
58 extern char HostName
[];
59 extern struct passwd
*ppp
;
60 extern char *attach_tty
, *attach_term
, *LoginName
, *preselect
;
61 extern int xflag
, dflag
, rflag
, quietflag
, adaptflag
;
62 extern struct mode attach_Mode
;
63 extern struct NewWindow nwin_options
;
64 extern int MasterPid
, attach_fd
;
68 extern int multiattach
, multi_uid
, own_uid
;
69 extern int tty_mode
, tty_oldmode
;
71 static int multipipe
[2];
76 static int ContinuePlease
;
79 AttachSigCont SIGDEFARG
88 * Send message to a screen backend.
89 * returns 1 if we could attach one, or 0 if none.
90 * Understands MSG_ATTACH, MSG_DETACH, MSG_POW_DETACH
91 * MSG_CONT, MSG_WINCH and nothing else!
93 * if type == MSG_ATTACH and sockets are used, attaches
102 int r
, l
= sizeof(*m
);
105 if (m
->type
== MSG_ATTACH
)
106 return SendAttachMsg(s
, m
, attach_fd
);
111 r
= write(s
, (char *)m
+ (sizeof(*m
) - l
), l
);
112 if (r
== -1 && errno
== EINTR
)
114 if (r
== -1 || r
== 0)
131 debug2("Attach: how=%d, tty=%s\n", how
, attach_tty
);
134 while ((how
== MSG_ATTACH
|| how
== MSG_CONT
) && multiattach
)
139 Panic(errno
, "pipe");
140 if (chmod(attach_tty
, 0666))
141 Panic(errno
, "chmod %s", attach_tty
);
142 tty_oldmode
= tty_mode
;
143 eff_uid
= -1; /* make UserContext fork */
144 real_uid
= multi_uid
;
145 if ((ret
= UserContext()) <= 0)
151 Panic(errno
, "UserContext");
153 read(multipipe
[0], &dummy
, 1);
154 if (tty_oldmode
>= 0)
156 chmod(attach_tty
, tty_oldmode
);
167 kill(getpid(), SIGTSTP
);
170 if (ret
== SIG_POWER_BYE
)
175 if ((ppid
= getppid()) > 1)
192 # else /* USE_SETEUID */
193 if ((how
== MSG_ATTACH
|| how
== MSG_CONT
) && multiattach
)
195 real_uid
= multi_uid
;
197 #ifdef HAVE_SETRESUID
198 if (setresuid(multi_uid
, own_uid
, multi_uid
))
199 Panic(errno
, "setresuid");
204 if (chmod(attach_tty
, 0666))
205 Panic(errno
, "chmod %s", attach_tty
);
206 tty_oldmode
= tty_mode
;
208 # endif /* USE_SETEUID */
209 #endif /* MULTIUSER */
211 bzero((char *) &m
, sizeof(m
));
213 m
.protocol_revision
= MSG_REVISION
;
214 strncpy(m
.m_tty
, attach_tty
, sizeof(m
.m_tty
) - 1);
215 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
217 if (how
== MSG_WINCH
)
219 if ((lasts
= MakeClientSocket(0)) >= 0)
221 WriteMessage(lasts
, &m
);
229 if ((lasts
= MakeClientSocket(0)) < 0)
231 Panic(0, "Sorry, cannot contact session \"%s\" again.\r\n",
237 n
= FindSocket(&lasts
, (int *)0, (int *)0, SockMatch
);
241 if (rflag
&& (rflag
& 1) == 0)
245 Panic(0, SockMatch
&& *SockMatch
? "There is no screen to be %sed matching %s." : "There is no screen to be %sed.",
257 Panic(0, "Type \"screen [-d] -r [pid.]tty.host\" to resume one of them.");
263 * Go in UserContext. Advantage is, you can kill your attacher
264 * when things go wrong. Any disadvantages? jw.
265 * Do this before the attach to prevent races!
271 #if defined(MULTIUSER) && defined(USE_SETEUID)
274 /* This call to xsetuid should also set the saved uid */
275 xseteuid(real_uid
); /* multi_uid, allow backend to send signals */
282 debug2("Attach: uid %d euid %d\n", (int)getuid(), (int)geteuid());
284 for (s
= SockName
; *s
; s
++)
286 if (*s
> '9' || *s
< '0')
288 MasterPid
= 10 * MasterPid
+ (*s
- '0');
290 debug1("Attach decided, it is '%s'\n", SockPath
);
291 debug1("Attach found MasterPid == %d\n", MasterPid
);
292 if (stat(SockPath
, &st
) == -1)
293 Panic(errno
, "stat %s", SockPath
);
294 if ((st
.st_mode
& 0600) != 0600)
295 Panic(0, "Socket is in wrong mode (%03o)", (int)st
.st_mode
);
298 * Change: if -x or -r ignore failing -d
300 if ((xflag
|| rflag
) && dflag
&& (st
.st_mode
& 0700) == 0600)
304 * Without -x, the mode must match.
305 * With -x the mode is irrelevant unless -d.
307 if ((dflag
|| !xflag
) && (st
.st_mode
& 0700) != (dflag
? 0700 : 0600))
308 Panic(0, "That screen is %sdetached.", dflag
? "already " : "not ");
311 (how
== MSG_DETACH
|| how
== MSG_POW_DETACH
))
313 m
.m
.detach
.dpid
= getpid();
314 strncpy(m
.m
.detach
.duser
, LoginName
, sizeof(m
.m
.detach
.duser
) - 1);
315 m
.m
.detach
.duser
[sizeof(m
.m
.detach
.duser
) - 1] = 0;
318 m
.type
= MSG_POW_DETACH
;
322 /* If there is no password for the session, or the user enters the correct
323 * password, then we get a SIGCONT. Otherwise we get a SIG_BYE */
324 signal(SIGCONT
, AttachSigCont
);
325 if (WriteMessage(lasts
, &m
))
326 Panic(errno
, "WriteMessage");
328 while (!ContinuePlease
)
329 pause(); /* wait for SIGCONT */
330 signal(SIGCONT
, SIG_DFL
);
332 if (how
!= MSG_ATTACH
)
333 return 0; /* we detached it. jw. */
334 sleep(1); /* we dont want to overrun our poor backend. jw. */
335 if ((lasts
= MakeClientSocket(0)) == -1)
336 Panic(0, "Cannot contact screen again. Sigh.");
340 ASSERT(how
== MSG_ATTACH
|| how
== MSG_CONT
);
341 strncpy(m
.m
.attach
.envterm
, attach_term
, sizeof(m
.m
.attach
.envterm
) - 1);
342 m
.m
.attach
.envterm
[sizeof(m
.m
.attach
.envterm
) - 1] = 0;
343 debug1("attach: sending %d bytes... ", (int)sizeof(m
));
345 strncpy(m
.m
.attach
.auser
, LoginName
, sizeof(m
.m
.attach
.auser
) - 1);
346 m
.m
.attach
.auser
[sizeof(m
.m
.attach
.auser
) - 1] = 0;
347 m
.m
.attach
.esc
= DefaultEsc
;
348 m
.m
.attach
.meta_esc
= DefaultMetaEsc
;
349 strncpy(m
.m
.attach
.preselect
, preselect
? preselect
: "", sizeof(m
.m
.attach
.preselect
) - 1);
350 m
.m
.attach
.preselect
[sizeof(m
.m
.attach
.preselect
) - 1] = 0;
351 m
.m
.attach
.apid
= getpid();
352 m
.m
.attach
.adaptflag
= adaptflag
;
353 m
.m
.attach
.lines
= m
.m
.attach
.columns
= 0;
354 if ((s
= getenv("LINES")))
355 m
.m
.attach
.lines
= atoi(s
);
356 if ((s
= getenv("COLUMNS")))
357 m
.m
.attach
.columns
= atoi(s
);
358 m
.m
.attach
.encoding
= nwin_options
.encoding
> 0 ? nwin_options
.encoding
+ 1 : 0;
363 m
.m
.attach
.detachfirst
= MSG_POW_DETACH
;
367 m
.m
.attach
.detachfirst
= MSG_DETACH
;
370 m
.m
.attach
.detachfirst
= MSG_ATTACH
;
373 /* setup CONT signal handler to repair the terminal mode */
374 if (multi
&& (how
== MSG_ATTACH
|| how
== MSG_CONT
))
375 signal(SIGCONT
, AttachSigCont
);
378 if (WriteMessage(lasts
, &m
))
379 Panic(errno
, "WriteMessage");
381 debug1("Attach(%d): sent\n", m
.type
);
383 if (multi
&& (how
== MSG_ATTACH
|| how
== MSG_CONT
))
385 while (!ContinuePlease
)
386 pause(); /* wait for SIGCONT */
387 signal(SIGCONT
, SIG_DFL
);
393 if (tty_oldmode
>= 0)
394 if (chmod(attach_tty
, tty_oldmode
))
395 Panic(errno
, "chmod %s", attach_tty
);
406 static int AttacherPanic
= 0;
410 AttacherChld SIGDEFARG
418 AttacherSigAlarm SIGDEFARG
421 static int tick_cnt
= 0;
422 if ((tick_cnt
= (tick_cnt
+ 1) % 4) == 0)
429 * the frontend's Interrupt handler
430 * we forward SIGINT to the poor backend
433 AttacherSigInt SIGDEFARG
435 signal(SIGINT
, AttacherSigInt
);
436 Kill(MasterPid
, SIGINT
);
441 * Unfortunatelly this is also the SIGHUP handler, so we have to
442 * check if the backend is already detached.
446 AttacherFinit SIGDEFARG
452 debug("AttacherFinit();\n");
453 signal(SIGHUP
, SIG_IGN
);
454 /* Check if signal comes from backend */
455 if (stat(SockPath
, &statb
) == 0 && (statb
.st_mode
& 0777) != 0600)
457 debug("Detaching backend!\n");
458 bzero((char *) &m
, sizeof(m
));
459 strncpy(m
.m_tty
, attach_tty
, sizeof(m
.m_tty
) - 1);
460 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
461 debug1("attach_tty is %s\n", attach_tty
);
462 m
.m
.detach
.dpid
= getpid();
464 m
.protocol_revision
= MSG_REVISION
;
465 if ((s
= MakeClientSocket(0)) >= 0)
472 if (tty_oldmode
>= 0)
475 chmod(attach_tty
, tty_oldmode
);
484 AttacherFinitBye SIGDEFARG
487 debug("AttacherFintBye()\n");
488 #if defined(MULTIUSER) && !defined(USE_SETEUID)
498 /* we don't want to disturb init (even if we were root), eh? jw */
499 if ((ppid
= getppid()) > 1)
500 Kill(ppid
, SIGHUP
); /* carefully say good bye. jw. */
506 #if defined(DEBUG) && defined(SIG_NODEBUG)
508 AttacherNoDebug SIGDEFARG
510 debug("AttacherNoDebug()\n");
511 signal(SIG_NODEBUG
, AttacherNoDebug
);
514 debug("debug: closing debug file.\n");
521 #endif /* SIG_NODEBUG */
523 static int SuspendPlease
;
528 debug("SigStop()\n");
534 static int LockPlease
;
540 signal(SIG_LOCK
, DoLock
);
548 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
549 static int SigWinchPlease
;
552 AttacherWinch SIGDEFARG
554 debug("AttacherWinch()\n");
562 * Attacher loop - no return
568 signal(SIGHUP
, AttacherFinit
);
569 signal(SIG_BYE
, AttacherFinit
);
571 signal(SIG_POWER_BYE
, AttacherFinitBye
);
573 #if defined(DEBUG) && defined(SIG_NODEBUG)
574 signal(SIG_NODEBUG
, AttacherNoDebug
);
577 signal(SIG_LOCK
, DoLock
);
579 signal(SIGINT
, AttacherSigInt
);
581 signal(SIG_STOP
, SigStop
);
583 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
584 signal(SIGWINCH
, AttacherWinch
);
587 signal(SIGCHLD
, AttacherChld
);
589 debug("attacher: going for a nap.\n");
596 signal(SIGALRM
, AttacherSigAlarm
);
600 if (kill(MasterPid
, 0) < 0 && errno
!= EPERM
)
602 debug1("attacher: Panic! MasterPid %d does not exist.\n", MasterPid
);
607 fcntl(0, F_SETFL
, 0);
608 SetTTY(0, &attach_Mode
);
609 printf("\nSuddenly the Dungeon collapses!! - You die...\n");
616 #if defined(MULTIUSER) && !defined(USE_SETEUID)
620 signal(SIGTSTP
, SIG_DFL
);
621 debug("attacher: killing myself SIGTSTP\n");
622 kill(getpid(), SIGTSTP
);
623 debug("attacher: continuing from stop\n");
624 signal(SIG_STOP
, SigStop
);
625 (void) Attach(MSG_CONT
);
632 #if defined(MULTIUSER) && !defined(USE_SETEUID)
638 signal(SIG_LOCK
, DoLock
);
640 (void) Attach(MSG_CONT
);
643 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
648 signal(SIGWINCH
, AttacherWinch
);
650 (void) Attach(MSG_WINCH
);
652 #endif /* SIGWINCH */
658 /* ADDED by Rainer Pruy 10/15/87 */
659 /* POLISHED by mls. 03/10/91 */
661 static char LockEnd
[] = "Welcome back to screen !!\n";
666 int ppid
= getppid();
683 sigret_t (*sigs
[NSIG
])__P(SIGPROTOARG
);
685 for (sig
= 1; sig
< NSIG
; sig
++)
686 sigs
[sig
] = signal(sig
, sig
== SIGCHLD
? SIG_DFL
: SIG_IGN
);
687 signal(SIGHUP
, LockHup
);
690 prg
= getenv("LOCKPRG");
691 if (prg
&& strcmp(prg
, "builtin") && !access(prg
, X_OK
))
693 signal(SIGCHLD
, SIG_DFL
);
694 debug1("lockterminal: '%s' seems executable, execl it!\n", prg
);
695 if ((pid
= fork()) == 0)
702 setuid(real_uid
); /* this should be done already */
704 closeallfiles(0); /* important: /etc/shadow may be open */
705 execl(prg
, "SCREEN-LOCK", NULL
);
709 Msg(errno
, "Cannot lock terminal - fork failed");
720 signal(SIGCHLD
, SIG_DFL
);
723 while (((wret
= wait(&wstat
)) != pid
) ||
724 ((wret
== -1) && (errno
== EINTR
))
733 else if (WTERMSIG(wstat
) != 0)
735 fprintf(stderr
, "Lock: %s: Killed by signal: %d%s\n", prg
,
736 WTERMSIG(wstat
), WIFCORESIG(wstat
) ? " (Core dumped)" : "");
739 else if (WEXITSTATUS(wstat
))
741 debug2("Lock: %s: return code %d\n", prg
, WEXITSTATUS(wstat
));
751 debug1("lockterminal: '%s' seems NOT executable, we use our builtin\n", prg
);
755 debug("lockterminal: using buitin.\n");
757 screen_builtin_lck();
760 for (sig
= 1; sig
< NSIG
; sig
++)
762 if (sigs
[sig
] != (sigret_t(*)__P(SIGPROTOARG
)) -1)
763 signal(sig
, sigs
[sig
]);
770 * PAM support by Pablo Averbuj <pablo@averbuj.com>
773 #include <security/pam_appl.h>
775 static int PAM_conv
__P((int, const struct pam_message
**, struct pam_response
**, void *));
778 PAM_conv(num_msg
, msg
, resp
, appdata_ptr
)
780 const struct pam_message
**msg
;
781 struct pam_response
**resp
;
785 struct pam_response
*reply
= NULL
;
787 reply
= malloc(sizeof(struct pam_response
)*num_msg
);
790 #define COPY_STRING(s) (s) ? strdup(s) : NULL
792 for (replies
= 0; replies
< num_msg
; replies
++)
794 switch (msg
[replies
]->msg_style
)
796 case PAM_PROMPT_ECHO_OFF
:
798 reply
[replies
].resp_retcode
= PAM_SUCCESS
;
799 reply
[replies
].resp
= appdata_ptr
? strdup((char *)appdata_ptr
) : 0;
802 /* ignore the informational mesage */
803 /* but first clear out any drek left by malloc */
804 reply
[replies
].resp
= NULL
;
806 case PAM_PROMPT_ECHO_ON
:
807 /* user name given to PAM already */
810 /* unknown or PAM_ERROR_MSG */
819 static struct pam_conv PAM_conversation
= {
827 /* -- original copyright by Luigi Cannelloni 1985 (luigi@faui70.UUCP) -- */
831 char fullname
[100], *cp1
, message
[100 + 100];
833 pam_handle_t
*pamh
= 0;
836 char *pass
, mypass
[16 + 1], salt
[3];
840 pass
= ppp
->pw_passwd
;
841 if (pass
== 0 || *pass
== 0)
843 if ((pass
= getpass("Key: ")))
845 strncpy(mypass
, pass
, sizeof(mypass
) - 1);
846 mypass
[sizeof(mypass
) - 1] = 0;
849 if ((pass
= getpass("Again: ")))
851 if (strcmp(mypass
, pass
))
853 fprintf(stderr
, "Passwords don't match.\007\n");
861 fprintf(stderr
, "Getpass error.\007\n");
866 salt
[0] = 'A' + (int)(time(0) % 26);
867 salt
[1] = 'A' + (int)((time(0) >> 6) % 26);
869 pass
= crypt(mypass
, salt
);
870 pass
= ppp
->pw_passwd
= SaveStr(pass
);
874 debug("screen_builtin_lck looking in gcos field\n");
875 strncpy(fullname
, ppp
->pw_gecos
, sizeof(fullname
) - 9);
876 fullname
[sizeof(fullname
) - 9] = 0;
878 if ((cp1
= index(fullname
, ',')) != NULL
)
880 if ((cp1
= index(fullname
, '&')) != NULL
)
882 strncpy(cp1
, ppp
->pw_name
, 8);
884 if (*cp1
>= 'a' && *cp1
<= 'z')
888 sprintf(message
, "Screen used by %s%s<%s> on %s.\nPassword:\007",
889 fullname
, fullname
[0] ? " " : "", ppp
->pw_name
, HostName
);
891 /* loop here to wait for correct password */
894 debug("screen_builtin_lck awaiting password\n");
896 if ((cp1
= getpass(message
)) == NULL
)
898 AttacherFinit(SIGARG
);
902 PAM_conversation
.appdata_ptr
= cp1
;
903 pam_error
= pam_start("screen", ppp
->pw_name
, &PAM_conversation
, &pamh
);
904 if (pam_error
!= PAM_SUCCESS
)
905 AttacherFinit(SIGARG
); /* goodbye */
906 pam_error
= pam_authenticate(pamh
, 0);
907 pam_end(pamh
, pam_error
);
908 PAM_conversation
.appdata_ptr
= 0;
909 if (pam_error
== PAM_SUCCESS
)
912 if (!strncmp(crypt(cp1
, pass
), pass
, strlen(pass
)))
915 debug("screen_builtin_lck: NO!!!!!\n");
916 bzero(cp1
, strlen(cp1
));
918 bzero(cp1
, strlen(cp1
));
919 debug("password ok.\n");
926 SendCmdMessage(sty
, match
, av
)
938 i
= FindSocket(&s
, (int *)0, (int *)0, match
);
940 Panic(0, "No screen session found.");
942 Panic(0, "Use -S to specify a session.");
947 if (strlen(sty
) > NAME_MAX
)
950 if (strlen(sty
) > 2 * MAXSTR
- 1)
951 sty
[2 * MAXSTR
- 1] = 0;
952 sprintf(SockPath
+ strlen(SockPath
), "/%s", sty
);
953 if ((s
= MakeClientSocket(1)) == -1)
956 bzero((char *)&m
, sizeof(m
));
957 m
.type
= MSG_COMMAND
;
960 strncpy(m
.m_tty
, attach_tty
, sizeof(m
.m_tty
) - 1);
961 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
965 for (; *av
&& n
< MAXARGS
- 1; ++av
, ++n
)
967 len
= strlen(*av
) + 1;
968 if (p
+ len
>= m
.m
.command
.cmd
+ sizeof(m
.m
.command
.cmd
) - 1)
974 m
.m
.command
.nargs
= n
;
975 strncpy(m
.m
.attach
.auser
, LoginName
, sizeof(m
.m
.attach
.auser
) - 1);
976 m
.m
.command
.auser
[sizeof(m
.m
.command
.auser
) - 1] = 0;
977 m
.protocol_revision
= MSG_REVISION
;
978 strncpy(m
.m
.command
.preselect
, preselect
? preselect
: "", sizeof(m
.m
.command
.preselect
) - 1);
979 m
.m
.command
.preselect
[sizeof(m
.m
.command
.preselect
) - 1] = 0;
980 m
.m
.command
.apid
= getpid();
981 debug1("SendCommandMsg writing '%s'\n", m
.m
.command
.cmd
);
982 if (WriteMessage(s
, &m
))