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
);
55 static sigret_t AttachSigCont
__P(SIGPROTOARG
);
58 extern int real_uid
, real_gid
, eff_uid
, eff_gid
;
59 extern char *SockName
, *SockMatch
, SockPath
[];
60 extern char HostName
[];
61 extern struct passwd
*ppp
;
62 extern char *attach_tty
, *attach_term
, *LoginName
, *preselect
;
63 extern int xflag
, dflag
, rflag
, quietflag
, adaptflag
;
64 extern struct mode attach_Mode
;
65 extern struct NewWindow nwin_options
;
66 extern int MasterPid
, attach_fd
;
70 extern int multiattach
, multi_uid
, own_uid
;
71 extern int tty_mode
, tty_oldmode
;
73 static int multipipe
[2];
79 static int ContinuePlease
;
82 AttachSigCont SIGDEFARG
92 * Send message to a screen backend.
93 * returns 1 if we could attach one, or 0 if none.
94 * Understands MSG_ATTACH, MSG_DETACH, MSG_POW_DETACH
95 * MSG_CONT, MSG_WINCH and nothing else!
97 * if type == MSG_ATTACH and sockets are used, attaches
106 int r
, l
= sizeof(*m
);
109 if (m
->type
== MSG_ATTACH
)
110 return SendAttachMsg(s
, m
, attach_fd
);
115 r
= write(s
, (char *)m
+ (sizeof(*m
) - l
), l
);
116 if (r
== -1 && errno
== EINTR
)
118 if (r
== -1 || r
== 0)
135 debug2("Attach: how=%d, tty=%s\n", how
, attach_tty
);
138 while ((how
== MSG_ATTACH
|| how
== MSG_CONT
) && multiattach
)
143 Panic(errno
, "pipe");
144 if (chmod(attach_tty
, 0666))
145 Panic(errno
, "chmod %s", attach_tty
);
146 tty_oldmode
= tty_mode
;
147 eff_uid
= -1; /* make UserContext fork */
148 real_uid
= multi_uid
;
149 if ((ret
= UserContext()) <= 0)
155 Panic(errno
, "UserContext");
157 read(multipipe
[0], &dummy
, 1);
158 if (tty_oldmode
>= 0)
160 chmod(attach_tty
, tty_oldmode
);
171 kill(getpid(), SIGTSTP
);
174 if (ret
== SIG_POWER_BYE
)
179 if ((ppid
= getppid()) > 1)
196 # else /* USE_SETEUID */
197 if ((how
== MSG_ATTACH
|| how
== MSG_CONT
) && multiattach
)
199 real_uid
= multi_uid
;
201 #ifdef HAVE_SETRESUID
202 if (setresuid(multi_uid
, own_uid
, multi_uid
))
203 Panic(errno
, "setresuid");
208 if (chmod(attach_tty
, 0666))
209 Panic(errno
, "chmod %s", attach_tty
);
210 tty_oldmode
= tty_mode
;
212 # endif /* USE_SETEUID */
213 #endif /* MULTIUSER */
215 bzero((char *) &m
, sizeof(m
));
217 m
.protocol_revision
= MSG_REVISION
;
218 strncpy(m
.m_tty
, attach_tty
, sizeof(m
.m_tty
) - 1);
219 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
221 if (how
== MSG_WINCH
)
223 if ((lasts
= MakeClientSocket(0)) >= 0)
225 WriteMessage(lasts
, &m
);
233 if ((lasts
= MakeClientSocket(0)) < 0)
235 Panic(0, "Sorry, cannot contact session \"%s\" again.\r\n",
241 n
= FindSocket(&lasts
, (int *)0, (int *)0, SockMatch
);
245 if (rflag
&& (rflag
& 1) == 0)
249 Panic(0, SockMatch
&& *SockMatch
? "There is no screen to be %sed matching %s." : "There is no screen to be %sed.",
261 Panic(0, "Type \"screen [-d] -r [pid.]tty.host\" to resume one of them.");
267 * Go in UserContext. Advantage is, you can kill your attacher
268 * when things go wrong. Any disadvantages? jw.
269 * Do this before the attach to prevent races!
275 #if defined(MULTIUSER) && defined(USE_SETEUID)
278 /* This call to xsetuid should also set the saved uid */
279 xseteuid(real_uid
); /* multi_uid, allow backend to send signals */
286 debug2("Attach: uid %d euid %d\n", (int)getuid(), (int)geteuid());
288 for (s
= SockName
; *s
; s
++)
290 if (*s
> '9' || *s
< '0')
292 MasterPid
= 10 * MasterPid
+ (*s
- '0');
294 debug1("Attach decided, it is '%s'\n", SockPath
);
295 debug1("Attach found MasterPid == %d\n", MasterPid
);
296 if (stat(SockPath
, &st
) == -1)
297 Panic(errno
, "stat %s", SockPath
);
298 if ((st
.st_mode
& 0600) != 0600)
299 Panic(0, "Socket is in wrong mode (%03o)", (int)st
.st_mode
);
302 * Change: if -x or -r ignore failing -d
304 if ((xflag
|| rflag
) && dflag
&& (st
.st_mode
& 0700) == 0600)
308 * Without -x, the mode must match.
309 * With -x the mode is irrelevant unless -d.
311 if ((dflag
|| !xflag
) && (st
.st_mode
& 0700) != (dflag
? 0700 : 0600))
312 Panic(0, "That screen is %sdetached.", dflag
? "already " : "not ");
315 (how
== MSG_DETACH
|| how
== MSG_POW_DETACH
))
317 m
.m
.detach
.dpid
= getpid();
318 strncpy(m
.m
.detach
.duser
, LoginName
, sizeof(m
.m
.detach
.duser
) - 1);
319 m
.m
.detach
.duser
[sizeof(m
.m
.detach
.duser
) - 1] = 0;
322 m
.type
= MSG_POW_DETACH
;
326 /* If there is no password for the session, or the user enters the correct
327 * password, then we get a SIGCONT. Otherwise we get a SIG_BYE */
328 signal(SIGCONT
, AttachSigCont
);
329 if (WriteMessage(lasts
, &m
))
330 Panic(errno
, "WriteMessage");
332 while (!ContinuePlease
)
333 pause(); /* wait for SIGCONT */
334 signal(SIGCONT
, SIG_DFL
);
336 if (how
!= MSG_ATTACH
)
337 return 0; /* we detached it. jw. */
338 sleep(1); /* we dont want to overrun our poor backend. jw. */
339 if ((lasts
= MakeClientSocket(0)) == -1)
340 Panic(0, "Cannot contact screen again. Sigh.");
344 ASSERT(how
== MSG_ATTACH
|| how
== MSG_CONT
);
345 strncpy(m
.m
.attach
.envterm
, attach_term
, sizeof(m
.m
.attach
.envterm
) - 1);
346 m
.m
.attach
.envterm
[sizeof(m
.m
.attach
.envterm
) - 1] = 0;
347 debug1("attach: sending %d bytes... ", (int)sizeof(m
));
349 strncpy(m
.m
.attach
.auser
, LoginName
, sizeof(m
.m
.attach
.auser
) - 1);
350 m
.m
.attach
.auser
[sizeof(m
.m
.attach
.auser
) - 1] = 0;
351 m
.m
.attach
.esc
= DefaultEsc
;
352 m
.m
.attach
.meta_esc
= DefaultMetaEsc
;
353 strncpy(m
.m
.attach
.preselect
, preselect
? preselect
: "", sizeof(m
.m
.attach
.preselect
) - 1);
354 m
.m
.attach
.preselect
[sizeof(m
.m
.attach
.preselect
) - 1] = 0;
355 m
.m
.attach
.apid
= getpid();
356 m
.m
.attach
.adaptflag
= adaptflag
;
357 m
.m
.attach
.lines
= m
.m
.attach
.columns
= 0;
358 if ((s
= getenv("LINES")))
359 m
.m
.attach
.lines
= atoi(s
);
360 if ((s
= getenv("COLUMNS")))
361 m
.m
.attach
.columns
= atoi(s
);
362 m
.m
.attach
.encoding
= nwin_options
.encoding
> 0 ? nwin_options
.encoding
+ 1 : 0;
367 m
.m
.attach
.detachfirst
= MSG_POW_DETACH
;
371 m
.m
.attach
.detachfirst
= MSG_DETACH
;
374 m
.m
.attach
.detachfirst
= MSG_ATTACH
;
377 /* setup CONT signal handler to repair the terminal mode */
378 if (multi
&& (how
== MSG_ATTACH
|| how
== MSG_CONT
))
379 signal(SIGCONT
, AttachSigCont
);
382 if (WriteMessage(lasts
, &m
))
383 Panic(errno
, "WriteMessage");
385 debug1("Attach(%d): sent\n", m
.type
);
387 if (multi
&& (how
== MSG_ATTACH
|| how
== MSG_CONT
))
389 while (!ContinuePlease
)
390 pause(); /* wait for SIGCONT */
391 signal(SIGCONT
, SIG_DFL
);
397 if (tty_oldmode
>= 0)
398 if (chmod(attach_tty
, tty_oldmode
))
399 Panic(errno
, "chmod %s", attach_tty
);
410 #if defined(DEBUG) || !defined(DO_NOT_POLL_MASTER)
411 static int AttacherPanic
= 0;
416 AttacherChld SIGDEFARG
424 AttacherSigAlarm SIGDEFARG
427 static int tick_cnt
= 0;
428 if ((tick_cnt
= (tick_cnt
+ 1) % 4) == 0)
435 * the frontend's Interrupt handler
436 * we forward SIGINT to the poor backend
439 AttacherSigInt SIGDEFARG
441 signal(SIGINT
, AttacherSigInt
);
442 Kill(MasterPid
, SIGINT
);
447 * Unfortunatelly this is also the SIGHUP handler, so we have to
448 * check if the backend is already detached.
452 AttacherFinit SIGDEFARG
458 debug("AttacherFinit();\n");
459 signal(SIGHUP
, SIG_IGN
);
460 /* Check if signal comes from backend */
461 if (stat(SockPath
, &statb
) == 0 && (statb
.st_mode
& 0777) != 0600)
463 debug("Detaching backend!\n");
464 bzero((char *) &m
, sizeof(m
));
465 strncpy(m
.m_tty
, attach_tty
, sizeof(m
.m_tty
) - 1);
466 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
467 debug1("attach_tty is %s\n", attach_tty
);
468 m
.m
.detach
.dpid
= getpid();
470 m
.protocol_revision
= MSG_REVISION
;
471 if ((s
= MakeClientSocket(0)) >= 0)
478 if (tty_oldmode
>= 0)
481 chmod(attach_tty
, tty_oldmode
);
490 AttacherFinitBye SIGDEFARG
493 debug("AttacherFintBye()\n");
494 #if defined(MULTIUSER) && !defined(USE_SETEUID)
504 /* we don't want to disturb init (even if we were root), eh? jw */
505 if ((ppid
= getppid()) > 1)
506 Kill(ppid
, SIGHUP
); /* carefully say good bye. jw. */
512 #if defined(DEBUG) && defined(SIG_NODEBUG)
514 AttacherNoDebug SIGDEFARG
516 debug("AttacherNoDebug()\n");
517 signal(SIG_NODEBUG
, AttacherNoDebug
);
520 debug("debug: closing debug file.\n");
527 #endif /* SIG_NODEBUG */
529 static int SuspendPlease
;
534 debug("SigStop()\n");
540 static int LockPlease
;
546 signal(SIG_LOCK
, DoLock
);
554 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
555 static int SigWinchPlease
;
558 AttacherWinch SIGDEFARG
560 debug("AttacherWinch()\n");
568 * Attacher loop - no return
574 signal(SIGHUP
, AttacherFinit
);
575 signal(SIG_BYE
, AttacherFinit
);
577 signal(SIG_POWER_BYE
, AttacherFinitBye
);
579 #if defined(DEBUG) && defined(SIG_NODEBUG)
580 signal(SIG_NODEBUG
, AttacherNoDebug
);
583 signal(SIG_LOCK
, DoLock
);
585 signal(SIGINT
, AttacherSigInt
);
587 signal(SIG_STOP
, SigStop
);
589 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
590 signal(SIGWINCH
, AttacherWinch
);
593 signal(SIGCHLD
, AttacherChld
);
595 debug("attacher: going for a nap.\n");
602 #ifndef DO_NOT_POLL_MASTER
603 signal(SIGALRM
, AttacherSigAlarm
);
607 if (kill(MasterPid
, 0) < 0 && errno
!= EPERM
)
609 debug1("attacher: Panic! MasterPid %d does not exist.\n", MasterPid
);
615 #if defined(DEBUG) || !defined(DO_NOT_POLL_MASTER)
618 fcntl(0, F_SETFL
, 0);
619 SetTTY(0, &attach_Mode
);
620 printf("\nSuddenly the Dungeon collapses!! - You die...\n");
628 #if defined(MULTIUSER) && !defined(USE_SETEUID)
632 signal(SIGTSTP
, SIG_DFL
);
633 debug("attacher: killing myself SIGTSTP\n");
634 kill(getpid(), SIGTSTP
);
635 debug("attacher: continuing from stop\n");
636 signal(SIG_STOP
, SigStop
);
637 (void) Attach(MSG_CONT
);
644 #if defined(MULTIUSER) && !defined(USE_SETEUID)
650 signal(SIG_LOCK
, DoLock
);
652 (void) Attach(MSG_CONT
);
655 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
660 signal(SIGWINCH
, AttacherWinch
);
662 (void) Attach(MSG_WINCH
);
664 #endif /* SIGWINCH */
670 /* ADDED by Rainer Pruy 10/15/87 */
671 /* POLISHED by mls. 03/10/91 */
673 static char LockEnd
[] = "Welcome back to screen !!\n";
678 int ppid
= getppid();
695 sigret_t (*sigs
[NSIG
])__P(SIGPROTOARG
);
697 for (sig
= 1; sig
< NSIG
; sig
++)
698 sigs
[sig
] = signal(sig
, sig
== SIGCHLD
? SIG_DFL
: SIG_IGN
);
699 signal(SIGHUP
, LockHup
);
702 prg
= getenv("LOCKPRG");
703 if (prg
&& strcmp(prg
, "builtin") && !access(prg
, X_OK
))
705 signal(SIGCHLD
, SIG_DFL
);
706 debug1("lockterminal: '%s' seems executable, execl it!\n", prg
);
707 if ((pid
= fork()) == 0)
714 setuid(real_uid
); /* this should be done already */
716 closeallfiles(0); /* important: /etc/shadow may be open */
717 execl(prg
, "SCREEN-LOCK", NULL
);
721 Msg(errno
, "Cannot lock terminal - fork failed");
732 signal(SIGCHLD
, SIG_DFL
);
735 while (((wret
= wait(&wstat
)) != pid
) ||
736 ((wret
== -1) && (errno
== EINTR
))
745 else if (WTERMSIG(wstat
) != 0)
747 fprintf(stderr
, "Lock: %s: Killed by signal: %d%s\n", prg
,
748 WTERMSIG(wstat
), WIFCORESIG(wstat
) ? " (Core dumped)" : "");
751 else if (WEXITSTATUS(wstat
))
753 debug2("Lock: %s: return code %d\n", prg
, WEXITSTATUS(wstat
));
763 debug1("lockterminal: '%s' seems NOT executable, we use our builtin\n", prg
);
767 debug("lockterminal: using buitin.\n");
769 screen_builtin_lck();
772 for (sig
= 1; sig
< NSIG
; sig
++)
774 if (sigs
[sig
] != (sigret_t(*)__P(SIGPROTOARG
)) -1)
775 signal(sig
, sigs
[sig
]);
782 * PAM support by Pablo Averbuj <pablo@averbuj.com>
785 #include <security/pam_appl.h>
787 static int PAM_conv
__P((int, const struct pam_message
**, struct pam_response
**, void *));
790 PAM_conv(num_msg
, msg
, resp
, appdata_ptr
)
792 const struct pam_message
**msg
;
793 struct pam_response
**resp
;
797 struct pam_response
*reply
= NULL
;
799 reply
= malloc(sizeof(struct pam_response
)*num_msg
);
802 #define COPY_STRING(s) (s) ? strdup(s) : NULL
804 for (replies
= 0; replies
< num_msg
; replies
++)
806 switch (msg
[replies
]->msg_style
)
808 case PAM_PROMPT_ECHO_OFF
:
810 reply
[replies
].resp_retcode
= PAM_SUCCESS
;
811 reply
[replies
].resp
= appdata_ptr
? strdup((char *)appdata_ptr
) : 0;
814 /* ignore the informational mesage */
815 /* but first clear out any drek left by malloc */
816 reply
[replies
].resp
= NULL
;
818 case PAM_PROMPT_ECHO_ON
:
819 /* user name given to PAM already */
822 /* unknown or PAM_ERROR_MSG */
831 static struct pam_conv PAM_conversation
= {
839 /* -- original copyright by Luigi Cannelloni 1985 (luigi@faui70.UUCP) -- */
843 char fullname
[100], *cp1
, message
[100 + 100];
845 pam_handle_t
*pamh
= 0;
848 char *pass
, mypass
[16 + 1], salt
[3];
852 pass
= ppp
->pw_passwd
;
853 if (pass
== 0 || *pass
== 0)
855 if ((pass
= getpass("Key: ")))
857 strncpy(mypass
, pass
, sizeof(mypass
) - 1);
858 mypass
[sizeof(mypass
) - 1] = 0;
861 if ((pass
= getpass("Again: ")))
863 if (strcmp(mypass
, pass
))
865 fprintf(stderr
, "Passwords don't match.\007\n");
873 fprintf(stderr
, "Getpass error.\007\n");
878 salt
[0] = 'A' + (int)(time(0) % 26);
879 salt
[1] = 'A' + (int)((time(0) >> 6) % 26);
881 pass
= crypt(mypass
, salt
);
882 pass
= ppp
->pw_passwd
= SaveStr(pass
);
886 debug("screen_builtin_lck looking in gcos field\n");
887 strncpy(fullname
, ppp
->pw_gecos
, sizeof(fullname
) - 9);
888 fullname
[sizeof(fullname
) - 9] = 0;
890 if ((cp1
= index(fullname
, ',')) != NULL
)
892 if ((cp1
= index(fullname
, '&')) != NULL
)
894 strncpy(cp1
, ppp
->pw_name
, 8);
896 if (*cp1
>= 'a' && *cp1
<= 'z')
900 sprintf(message
, "Screen used by %s%s<%s> on %s.\nPassword:\007",
901 fullname
, fullname
[0] ? " " : "", ppp
->pw_name
, HostName
);
903 /* loop here to wait for correct password */
906 debug("screen_builtin_lck awaiting password\n");
908 if ((cp1
= getpass(message
)) == NULL
)
910 AttacherFinit(SIGARG
);
914 PAM_conversation
.appdata_ptr
= cp1
;
915 pam_error
= pam_start("screen", ppp
->pw_name
, &PAM_conversation
, &pamh
);
916 if (pam_error
!= PAM_SUCCESS
)
917 AttacherFinit(SIGARG
); /* goodbye */
918 pam_error
= pam_authenticate(pamh
, 0);
919 pam_end(pamh
, pam_error
);
920 PAM_conversation
.appdata_ptr
= 0;
921 if (pam_error
== PAM_SUCCESS
)
924 if (!strncmp(crypt(cp1
, pass
), pass
, strlen(pass
)))
927 debug("screen_builtin_lck: NO!!!!!\n");
928 bzero(cp1
, strlen(cp1
));
930 bzero(cp1
, strlen(cp1
));
931 debug("password ok.\n");
938 SendCmdMessage(sty
, match
, av
)
950 i
= FindSocket(&s
, (int *)0, (int *)0, match
);
952 Panic(0, "No screen session found.");
954 Panic(0, "Use -S to specify a session.");
959 if (strlen(sty
) > NAME_MAX
)
962 if (strlen(sty
) > 2 * MAXSTR
- 1)
963 sty
[2 * MAXSTR
- 1] = 0;
964 sprintf(SockPath
+ strlen(SockPath
), "/%s", sty
);
965 if ((s
= MakeClientSocket(1)) == -1)
968 bzero((char *)&m
, sizeof(m
));
969 m
.type
= MSG_COMMAND
;
972 strncpy(m
.m_tty
, attach_tty
, sizeof(m
.m_tty
) - 1);
973 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
977 for (; *av
&& n
< MAXARGS
- 1; ++av
, ++n
)
979 len
= strlen(*av
) + 1;
980 if (p
+ len
>= m
.m
.command
.cmd
+ sizeof(m
.m
.command
.cmd
) - 1)
986 m
.m
.command
.nargs
= n
;
987 strncpy(m
.m
.attach
.auser
, LoginName
, sizeof(m
.m
.attach
.auser
) - 1);
988 m
.m
.command
.auser
[sizeof(m
.m
.command
.auser
) - 1] = 0;
989 m
.protocol_revision
= MSG_REVISION
;
990 strncpy(m
.m
.command
.preselect
, preselect
? preselect
: "", sizeof(m
.m
.command
.preselect
) - 1);
991 m
.m
.command
.preselect
[sizeof(m
.m
.command
.preselect
) - 1] = 0;
992 m
.m
.command
.apid
= getpid();
993 debug1("SendCommandMsg writing '%s'\n", m
.m
.command
.cmd
);
994 if (WriteMessage(s
, &m
))