1 /* Copyright (c) 1993-2002
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Copyright (c) 1987 Oliver Laumann
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (see the file COPYING); if not, write to the
18 * Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 ****************************************************************
24 #include <sys/types.h>
26 #include <sys/ioctl.h>
35 static int WriteMessage
__P((int, struct msg
*));
36 static sigret_t AttacherSigInt
__P(SIGPROTOARG
);
37 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
38 static sigret_t AttacherWinch
__P(SIGPROTOARG
);
41 static sigret_t DoLock
__P(SIGPROTOARG
);
42 static void LockTerminal
__P((void));
43 static sigret_t LockHup
__P(SIGPROTOARG
);
44 static void screen_builtin_lck
__P((void));
47 static sigret_t AttacherChld
__P(SIGPROTOARG
);
50 static sigret_t AttachSigCont
__P(SIGPROTOARG
);
53 extern int real_uid
, real_gid
, eff_uid
, eff_gid
;
54 extern char *SockName
, *SockMatch
, SockPath
[];
55 extern struct passwd
*ppp
;
56 extern char *attach_tty
, *attach_term
, *LoginName
, *preselect
;
57 extern int xflag
, dflag
, rflag
, quietflag
, adaptflag
;
58 extern struct mode attach_Mode
;
59 extern struct NewWindow nwin_options
;
60 extern int MasterPid
, attach_fd
;
64 extern int multiattach
, multi_uid
, own_uid
;
65 extern int tty_mode
, tty_oldmode
;
67 static int multipipe
[2];
73 static int ContinuePlease
;
76 AttachSigCont SIGDEFARG
86 * Send message to a screen backend.
87 * returns 1 if we could attach one, or 0 if none.
88 * Understands MSG_ATTACH, MSG_DETACH, MSG_POW_DETACH
89 * MSG_CONT, MSG_WINCH and nothing else!
91 * if type == MSG_ATTACH and sockets are used, attaches
100 int r
, l
= sizeof(*m
);
103 if (m
->type
== MSG_ATTACH
)
104 return SendAttachMsg(s
, m
, attach_fd
);
109 r
= write(s
, (char *)m
+ (sizeof(*m
) - l
), l
);
110 if (r
== -1 && errno
== EINTR
)
112 if (r
== -1 || r
== 0)
129 debug2("Attach: how=%d, tty=%s\n", how
, attach_tty
);
132 while ((how
== MSG_ATTACH
|| how
== MSG_CONT
) && multiattach
)
137 Panic(errno
, "pipe");
138 if (chmod(attach_tty
, 0666))
139 Panic(errno
, "chmod %s", attach_tty
);
140 tty_oldmode
= tty_mode
;
141 eff_uid
= -1; /* make UserContext fork */
142 real_uid
= multi_uid
;
143 if ((ret
= UserContext()) <= 0)
149 Panic(errno
, "UserContext");
151 read(multipipe
[0], &dummy
, 1);
152 if (tty_oldmode
>= 0)
154 chmod(attach_tty
, tty_oldmode
);
165 kill(getpid(), SIGTSTP
);
168 if (ret
== SIG_POWER_BYE
)
173 if ((ppid
= getppid()) > 1)
190 # else /* USE_SETEUID */
191 if ((how
== MSG_ATTACH
|| how
== MSG_CONT
) && multiattach
)
193 real_uid
= multi_uid
;
195 #ifdef HAVE_SETRESUID
196 if (setresuid(multi_uid
, own_uid
, multi_uid
))
197 Panic(errno
, "setresuid");
202 if (chmod(attach_tty
, 0666))
203 Panic(errno
, "chmod %s", attach_tty
);
204 tty_oldmode
= tty_mode
;
206 # endif /* USE_SETEUID */
207 #endif /* MULTIUSER */
209 bzero((char *) &m
, sizeof(m
));
211 m
.protocol_revision
= MSG_REVISION
;
212 strncpy(m
.m_tty
, attach_tty
, sizeof(m
.m_tty
) - 1);
213 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
215 if (how
== MSG_WINCH
)
217 if ((lasts
= MakeClientSocket(0)) >= 0)
219 WriteMessage(lasts
, &m
);
227 if ((lasts
= MakeClientSocket(0)) < 0)
229 Panic(0, "Sorry, cannot contact session \"%s\" again.\r\n",
235 n
= FindSocket(&lasts
, (int *)0, (int *)0, SockMatch
);
239 if (rflag
&& (rflag
& 1) == 0)
243 Panic(0, SockMatch
&& *SockMatch
? "There is no screen to be %sed matching %s." : "There is no screen to be %sed.",
255 Panic(0, "Type \"screen [-d] -r [pid.]tty.host\" to resume one of them.");
261 * Go in UserContext. Advantage is, you can kill your attacher
262 * when things go wrong. Any disadvantages? jw.
263 * Do this before the attach to prevent races!
269 #if defined(MULTIUSER) && defined(USE_SETEUID)
272 /* This call to xsetuid should also set the saved uid */
273 xseteuid(real_uid
); /* multi_uid, allow backend to send signals */
280 debug2("Attach: uid %d euid %d\n", (int)getuid(), (int)geteuid());
282 for (s
= SockName
; *s
; s
++)
284 if (*s
> '9' || *s
< '0')
286 MasterPid
= 10 * MasterPid
+ (*s
- '0');
288 debug1("Attach decided, it is '%s'\n", SockPath
);
289 debug1("Attach found MasterPid == %d\n", MasterPid
);
290 if (stat(SockPath
, &st
) == -1)
291 Panic(errno
, "stat %s", SockPath
);
292 if ((st
.st_mode
& 0600) != 0600)
293 Panic(0, "Socket is in wrong mode (%03o)", (int)st
.st_mode
);
296 * Change: if -x or -r ignore failing -d
298 if ((xflag
|| rflag
) && dflag
&& (st
.st_mode
& 0700) == 0600)
302 * Without -x, the mode must match.
303 * With -x the mode is irrelevant unless -d.
305 if ((dflag
|| !xflag
) && (st
.st_mode
& 0700) != (dflag
? 0700 : 0600))
306 Panic(0, "That screen is %sdetached.", dflag
? "already " : "not ");
309 (how
== MSG_ATTACH
|| how
== MSG_DETACH
|| how
== MSG_POW_DETACH
))
311 m
.m
.detach
.dpid
= getpid();
312 strncpy(m
.m
.detach
.duser
, LoginName
, sizeof(m
.m
.detach
.duser
) - 1);
313 m
.m
.detach
.duser
[sizeof(m
.m
.detach
.duser
) - 1] = 0;
316 m
.type
= MSG_POW_DETACH
;
320 if (WriteMessage(lasts
, &m
))
321 Panic(errno
, "WriteMessage");
323 if (how
!= MSG_ATTACH
)
324 return 0; /* we detached it. jw. */
325 sleep(1); /* we dont want to overrun our poor backend. jw. */
326 if ((lasts
= MakeClientSocket(0)) == -1)
327 Panic(0, "Cannot contact screen again. Sigh.");
331 ASSERT(how
== MSG_ATTACH
|| how
== MSG_CONT
);
332 strncpy(m
.m
.attach
.envterm
, attach_term
, sizeof(m
.m
.attach
.envterm
) - 1);
333 m
.m
.attach
.envterm
[sizeof(m
.m
.attach
.envterm
) - 1] = 0;
334 debug1("attach: sending %d bytes... ", (int)sizeof(m
));
336 strncpy(m
.m
.attach
.auser
, LoginName
, sizeof(m
.m
.attach
.auser
) - 1);
337 m
.m
.attach
.auser
[sizeof(m
.m
.attach
.auser
) - 1] = 0;
338 m
.m
.attach
.esc
= DefaultEsc
;
339 m
.m
.attach
.meta_esc
= DefaultMetaEsc
;
340 strncpy(m
.m
.attach
.preselect
, preselect
? preselect
: "", sizeof(m
.m
.attach
.preselect
) - 1);
341 m
.m
.attach
.preselect
[sizeof(m
.m
.attach
.preselect
) - 1] = 0;
342 m
.m
.attach
.apid
= getpid();
343 m
.m
.attach
.adaptflag
= adaptflag
;
344 m
.m
.attach
.lines
= m
.m
.attach
.columns
= 0;
345 if ((s
= getenv("LINES")))
346 m
.m
.attach
.lines
= atoi(s
);
347 if ((s
= getenv("COLUMNS")))
348 m
.m
.attach
.columns
= atoi(s
);
349 m
.m
.attach
.encoding
= nwin_options
.encoding
> 0 ? nwin_options
.encoding
+ 1 : 0;
352 /* setup CONT signal handler to repair the terminal mode */
353 if (multi
&& (how
== MSG_ATTACH
|| how
== MSG_CONT
))
354 signal(SIGCONT
, AttachSigCont
);
357 if (WriteMessage(lasts
, &m
))
358 Panic(errno
, "WriteMessage");
360 debug1("Attach(%d): sent\n", m
.type
);
362 if (multi
&& (how
== MSG_ATTACH
|| how
== MSG_CONT
))
364 while (!ContinuePlease
)
365 pause(); /* wait for SIGCONT */
366 signal(SIGCONT
, SIG_DFL
);
372 if (tty_oldmode
>= 0)
373 if (chmod(attach_tty
, tty_oldmode
))
374 Panic(errno
, "chmod %s", attach_tty
);
385 #if defined(DEBUG) || !defined(DO_NOT_POLL_MASTER)
386 static int AttacherPanic
= 0;
391 AttacherChld SIGDEFARG
399 AttacherSigAlarm SIGDEFARG
402 static int tick_cnt
= 0;
403 if ((tick_cnt
= (tick_cnt
+ 1) % 4) == 0)
410 * the frontend's Interrupt handler
411 * we forward SIGINT to the poor backend
414 AttacherSigInt SIGDEFARG
416 signal(SIGINT
, AttacherSigInt
);
417 Kill(MasterPid
, SIGINT
);
422 * Unfortunatelly this is also the SIGHUP handler, so we have to
423 * check if the backend is already detached.
427 AttacherFinit SIGDEFARG
433 debug("AttacherFinit();\n");
434 signal(SIGHUP
, SIG_IGN
);
435 /* Check if signal comes from backend */
436 if (stat(SockPath
, &statb
) == 0 && (statb
.st_mode
& 0777) != 0600)
438 debug("Detaching backend!\n");
439 bzero((char *) &m
, sizeof(m
));
440 strncpy(m
.m_tty
, attach_tty
, sizeof(m
.m_tty
) - 1);
441 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
442 debug1("attach_tty is %s\n", attach_tty
);
443 m
.m
.detach
.dpid
= getpid();
445 m
.protocol_revision
= MSG_REVISION
;
446 if ((s
= MakeClientSocket(0)) >= 0)
453 if (tty_oldmode
>= 0)
456 chmod(attach_tty
, tty_oldmode
);
465 AttacherFinitBye SIGDEFARG
468 debug("AttacherFintBye()\n");
469 #if defined(MULTIUSER) && !defined(USE_SETEUID)
479 /* we don't want to disturb init (even if we were root), eh? jw */
480 if ((ppid
= getppid()) > 1)
481 Kill(ppid
, SIGHUP
); /* carefully say good bye. jw. */
487 #if defined(DEBUG) && defined(SIG_NODEBUG)
489 AttacherNoDebug SIGDEFARG
491 debug("AttacherNoDebug()\n");
492 signal(SIG_NODEBUG
, AttacherNoDebug
);
495 debug("debug: closing debug file.\n");
502 #endif /* SIG_NODEBUG */
504 static int SuspendPlease
;
509 debug("SigStop()\n");
515 static int LockPlease
;
521 signal(SIG_LOCK
, DoLock
);
529 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
530 static int SigWinchPlease
;
533 AttacherWinch SIGDEFARG
535 debug("AttacherWinch()\n");
543 * Attacher loop - no return
549 signal(SIGHUP
, AttacherFinit
);
550 signal(SIG_BYE
, AttacherFinit
);
552 signal(SIG_POWER_BYE
, AttacherFinitBye
);
554 #if defined(DEBUG) && defined(SIG_NODEBUG)
555 signal(SIG_NODEBUG
, AttacherNoDebug
);
558 signal(SIG_LOCK
, DoLock
);
560 signal(SIGINT
, AttacherSigInt
);
562 signal(SIG_STOP
, SigStop
);
564 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
565 signal(SIGWINCH
, AttacherWinch
);
568 signal(SIGCHLD
, AttacherChld
);
570 debug("attacher: going for a nap.\n");
577 #ifndef DO_NOT_POLL_MASTER
578 signal(SIGALRM
, AttacherSigAlarm
);
582 if (kill(MasterPid
, 0) < 0 && errno
!= EPERM
)
584 debug1("attacher: Panic! MasterPid %d does not exist.\n", MasterPid
);
590 #if defined(DEBUG) || !defined(DO_NOT_POLL_MASTER)
593 fcntl(0, F_SETFL
, 0);
594 SetTTY(0, &attach_Mode
);
595 printf("\nSuddenly the Dungeon collapses!! - You die...\n");
603 #if defined(MULTIUSER) && !defined(USE_SETEUID)
607 signal(SIGTSTP
, SIG_DFL
);
608 debug("attacher: killing myself SIGTSTP\n");
609 kill(getpid(), SIGTSTP
);
610 debug("attacher: continuing from stop\n");
611 signal(SIG_STOP
, SigStop
);
612 (void) Attach(MSG_CONT
);
619 #if defined(MULTIUSER) && !defined(USE_SETEUID)
625 signal(SIG_LOCK
, DoLock
);
627 (void) Attach(MSG_CONT
);
630 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
635 signal(SIGWINCH
, AttacherWinch
);
637 (void) Attach(MSG_WINCH
);
639 #endif /* SIGWINCH */
645 /* ADDED by Rainer Pruy 10/15/87 */
646 /* POLISHED by mls. 03/10/91 */
648 static char LockEnd
[] = "Welcome back to screen !!\n";
653 int ppid
= getppid();
670 sigret_t (*sigs
[NSIG
])__P(SIGPROTOARG
);
672 for (sig
= 1; sig
< NSIG
; sig
++)
673 sigs
[sig
] = signal(sig
, sig
== SIGCHLD
? SIG_DFL
: SIG_IGN
);
674 signal(SIGHUP
, LockHup
);
677 prg
= getenv("LOCKPRG");
678 if (prg
&& strcmp(prg
, "builtin") && !access(prg
, X_OK
))
680 signal(SIGCHLD
, SIG_DFL
);
681 debug1("lockterminal: '%s' seems executable, execl it!\n", prg
);
682 if ((pid
= fork()) == 0)
689 setuid(real_uid
); /* this should be done already */
691 closeallfiles(0); /* important: /etc/shadow may be open */
692 execl(prg
, "SCREEN-LOCK", NULL
);
696 Msg(errno
, "Cannot lock terminal - fork failed");
707 signal(SIGCHLD
, SIG_DFL
);
710 while (((wret
= wait(&wstat
)) != pid
) ||
711 ((wret
== -1) && (errno
== EINTR
))
720 else if (WTERMSIG(wstat
) != 0)
722 fprintf(stderr
, "Lock: %s: Killed by signal: %d%s\n", prg
,
723 WTERMSIG(wstat
), WIFCORESIG(wstat
) ? " (Core dumped)" : "");
726 else if (WEXITSTATUS(wstat
))
728 debug2("Lock: %s: return code %d\n", prg
, WEXITSTATUS(wstat
));
738 debug1("lockterminal: '%s' seems NOT executable, we use our builtin\n", prg
);
742 debug("lockterminal: using buitin.\n");
744 screen_builtin_lck();
747 for (sig
= 1; sig
< NSIG
; sig
++)
749 if (sigs
[sig
] != (sigret_t(*)__P(SIGPROTOARG
)) -1)
750 signal(sig
, sigs
[sig
]);
757 * PAM support by Pablo Averbuj <pablo@averbuj.com>
760 #include <security/pam_appl.h>
762 static int PAM_conv
__P((int, const struct pam_message
**, struct pam_response
**, void *));
765 PAM_conv(num_msg
, msg
, resp
, appdata_ptr
)
767 const struct pam_message
**msg
;
768 struct pam_response
**resp
;
772 struct pam_response
*reply
= NULL
;
774 reply
= malloc(sizeof(struct pam_response
)*num_msg
);
777 #define COPY_STRING(s) (s) ? strdup(s) : NULL
779 for (replies
= 0; replies
< num_msg
; replies
++)
781 switch (msg
[replies
]->msg_style
)
783 case PAM_PROMPT_ECHO_OFF
:
785 reply
[replies
].resp_retcode
= PAM_SUCCESS
;
786 reply
[replies
].resp
= appdata_ptr
? strdup((char *)appdata_ptr
) : 0;
789 /* ignore the informational mesage */
790 /* but first clear out any drek left by malloc */
791 reply
[replies
].resp
= NULL
;
793 case PAM_PROMPT_ECHO_ON
:
794 /* user name given to PAM already */
797 /* unknown or PAM_ERROR_MSG */
806 static struct pam_conv PAM_conversation
= {
814 /* -- original copyright by Luigi Cannelloni 1985 (luigi@faui70.UUCP) -- */
818 char fullname
[100], *cp1
, message
[100 + 100];
820 pam_handle_t
*pamh
= 0;
823 char *pass
, mypass
[16 + 1], salt
[3];
827 pass
= ppp
->pw_passwd
;
828 if (pass
== 0 || *pass
== 0)
830 if ((pass
= getpass("Key: ")))
832 strncpy(mypass
, pass
, sizeof(mypass
) - 1);
833 mypass
[sizeof(mypass
) - 1] = 0;
836 if ((pass
= getpass("Again: ")))
838 if (strcmp(mypass
, pass
))
840 fprintf(stderr
, "Passwords don't match.\007\n");
848 fprintf(stderr
, "Getpass error.\007\n");
853 salt
[0] = 'A' + (int)(time(0) % 26);
854 salt
[1] = 'A' + (int)((time(0) >> 6) % 26);
856 pass
= crypt(mypass
, salt
);
857 pass
= ppp
->pw_passwd
= SaveStr(pass
);
861 debug("screen_builtin_lck looking in gcos field\n");
862 strncpy(fullname
, ppp
->pw_gecos
, sizeof(fullname
) - 9);
863 fullname
[sizeof(fullname
) - 9] = 0;
865 if ((cp1
= index(fullname
, ',')) != NULL
)
867 if ((cp1
= index(fullname
, '&')) != NULL
)
869 strncpy(cp1
, ppp
->pw_name
, 8);
871 if (*cp1
>= 'a' && *cp1
<= 'z')
875 sprintf(message
, "Screen used by %s%s<%s>.\nPassword:\007",
876 fullname
, fullname
[0] ? " " : "", ppp
->pw_name
);
878 /* loop here to wait for correct password */
881 debug("screen_builtin_lck awaiting password\n");
883 if ((cp1
= getpass(message
)) == NULL
)
885 AttacherFinit(SIGARG
);
889 PAM_conversation
.appdata_ptr
= cp1
;
890 pam_error
= pam_start("screen", ppp
->pw_name
, &PAM_conversation
, &pamh
);
891 if (pam_error
!= PAM_SUCCESS
)
892 AttacherFinit(SIGARG
); /* goodbye */
893 pam_error
= pam_authenticate(pamh
, 0);
894 pam_end(pamh
, pam_error
);
895 PAM_conversation
.appdata_ptr
= 0;
896 if (pam_error
== PAM_SUCCESS
)
899 if (!strncmp(crypt(cp1
, pass
), pass
, strlen(pass
)))
902 debug("screen_builtin_lck: NO!!!!!\n");
903 bzero(cp1
, strlen(cp1
));
905 bzero(cp1
, strlen(cp1
));
906 debug("password ok.\n");
913 SendCmdMessage(sty
, match
, av
)
925 i
= FindSocket(&s
, (int *)0, (int *)0, match
);
927 Panic(0, "No screen session found.");
929 Panic(0, "Use -S to specify a session.");
934 if (strlen(sty
) > NAME_MAX
)
937 if (strlen(sty
) > 2 * MAXSTR
- 1)
938 sty
[2 * MAXSTR
- 1] = 0;
939 sprintf(SockPath
+ strlen(SockPath
), "/%s", sty
);
940 if ((s
= MakeClientSocket(1)) == -1)
943 bzero((char *)&m
, sizeof(m
));
944 m
.type
= MSG_COMMAND
;
947 strncpy(m
.m_tty
, attach_tty
, sizeof(m
.m_tty
) - 1);
948 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
952 for (; *av
&& n
< MAXARGS
- 1; ++av
, ++n
)
954 len
= strlen(*av
) + 1;
955 if (p
+ len
>= m
.m
.command
.cmd
+ sizeof(m
.m
.command
.cmd
) - 1)
961 m
.m
.command
.nargs
= n
;
962 strncpy(m
.m
.attach
.auser
, LoginName
, sizeof(m
.m
.attach
.auser
) - 1);
963 m
.m
.command
.auser
[sizeof(m
.m
.command
.auser
) - 1] = 0;
964 m
.protocol_revision
= MSG_REVISION
;
965 strncpy(m
.m
.command
.preselect
, preselect
? preselect
: "", sizeof(m
.m
.command
.preselect
) - 1);
966 m
.m
.command
.preselect
[sizeof(m
.m
.command
.preselect
) - 1] = 0;
967 m
.m
.command
.apid
= getpid();
968 debug1("SendCommandMsg writing '%s'\n", m
.m
.command
.cmd
);
969 if (WriteMessage(s
, &m
))