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_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 there is no password for the session, or the user enters the correct
321 * password, then we get a SIGCONT. Otherwise we get a SIG_BYE */
322 signal(SIGCONT
, AttachSigCont
);
323 if (WriteMessage(lasts
, &m
))
324 Panic(errno
, "WriteMessage");
326 while (!ContinuePlease
)
327 pause(); /* wait for SIGCONT */
328 signal(SIGCONT
, SIG_DFL
);
330 if (how
!= MSG_ATTACH
)
331 return 0; /* we detached it. jw. */
332 sleep(1); /* we dont want to overrun our poor backend. jw. */
333 if ((lasts
= MakeClientSocket(0)) == -1)
334 Panic(0, "Cannot contact screen again. Sigh.");
338 ASSERT(how
== MSG_ATTACH
|| how
== MSG_CONT
);
339 strncpy(m
.m
.attach
.envterm
, attach_term
, sizeof(m
.m
.attach
.envterm
) - 1);
340 m
.m
.attach
.envterm
[sizeof(m
.m
.attach
.envterm
) - 1] = 0;
341 debug1("attach: sending %d bytes... ", (int)sizeof(m
));
343 strncpy(m
.m
.attach
.auser
, LoginName
, sizeof(m
.m
.attach
.auser
) - 1);
344 m
.m
.attach
.auser
[sizeof(m
.m
.attach
.auser
) - 1] = 0;
345 m
.m
.attach
.esc
= DefaultEsc
;
346 m
.m
.attach
.meta_esc
= DefaultMetaEsc
;
347 strncpy(m
.m
.attach
.preselect
, preselect
? preselect
: "", sizeof(m
.m
.attach
.preselect
) - 1);
348 m
.m
.attach
.preselect
[sizeof(m
.m
.attach
.preselect
) - 1] = 0;
349 m
.m
.attach
.apid
= getpid();
350 m
.m
.attach
.adaptflag
= adaptflag
;
351 m
.m
.attach
.lines
= m
.m
.attach
.columns
= 0;
352 if ((s
= getenv("LINES")))
353 m
.m
.attach
.lines
= atoi(s
);
354 if ((s
= getenv("COLUMNS")))
355 m
.m
.attach
.columns
= atoi(s
);
356 m
.m
.attach
.encoding
= nwin_options
.encoding
> 0 ? nwin_options
.encoding
+ 1 : 0;
361 m
.m
.attach
.detachfirst
= MSG_POW_DETACH
;
365 m
.m
.attach
.detachfirst
= MSG_DETACH
;
368 m
.m
.attach
.detachfirst
= MSG_ATTACH
;
371 /* setup CONT signal handler to repair the terminal mode */
372 if (multi
&& (how
== MSG_ATTACH
|| how
== MSG_CONT
))
373 signal(SIGCONT
, AttachSigCont
);
376 if (WriteMessage(lasts
, &m
))
377 Panic(errno
, "WriteMessage");
379 debug1("Attach(%d): sent\n", m
.type
);
381 if (multi
&& (how
== MSG_ATTACH
|| how
== MSG_CONT
))
383 while (!ContinuePlease
)
384 pause(); /* wait for SIGCONT */
385 signal(SIGCONT
, SIG_DFL
);
391 if (tty_oldmode
>= 0)
392 if (chmod(attach_tty
, tty_oldmode
))
393 Panic(errno
, "chmod %s", attach_tty
);
404 #if defined(DEBUG) || !defined(DO_NOT_POLL_MASTER)
405 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 #ifndef DO_NOT_POLL_MASTER
597 signal(SIGALRM
, AttacherSigAlarm
);
601 if (kill(MasterPid
, 0) < 0 && errno
!= EPERM
)
603 debug1("attacher: Panic! MasterPid %d does not exist.\n", MasterPid
);
609 #if defined(DEBUG) || !defined(DO_NOT_POLL_MASTER)
612 fcntl(0, F_SETFL
, 0);
613 SetTTY(0, &attach_Mode
);
614 printf("\nSuddenly the Dungeon collapses!! - You die...\n");
622 #if defined(MULTIUSER) && !defined(USE_SETEUID)
626 signal(SIGTSTP
, SIG_DFL
);
627 debug("attacher: killing myself SIGTSTP\n");
628 kill(getpid(), SIGTSTP
);
629 debug("attacher: continuing from stop\n");
630 signal(SIG_STOP
, SigStop
);
631 (void) Attach(MSG_CONT
);
638 #if defined(MULTIUSER) && !defined(USE_SETEUID)
644 signal(SIG_LOCK
, DoLock
);
646 (void) Attach(MSG_CONT
);
649 #if defined(SIGWINCH) && defined(TIOCGWINSZ)
654 signal(SIGWINCH
, AttacherWinch
);
656 (void) Attach(MSG_WINCH
);
658 #endif /* SIGWINCH */
664 /* ADDED by Rainer Pruy 10/15/87 */
665 /* POLISHED by mls. 03/10/91 */
667 static char LockEnd
[] = "Welcome back to screen !!\n";
672 int ppid
= getppid();
689 sigret_t (*sigs
[NSIG
])__P(SIGPROTOARG
);
691 for (sig
= 1; sig
< NSIG
; sig
++)
692 sigs
[sig
] = signal(sig
, sig
== SIGCHLD
? SIG_DFL
: SIG_IGN
);
693 signal(SIGHUP
, LockHup
);
696 prg
= getenv("LOCKPRG");
697 if (prg
&& strcmp(prg
, "builtin") && !access(prg
, X_OK
))
699 signal(SIGCHLD
, SIG_DFL
);
700 debug1("lockterminal: '%s' seems executable, execl it!\n", prg
);
701 if ((pid
= fork()) == 0)
708 setuid(real_uid
); /* this should be done already */
710 closeallfiles(0); /* important: /etc/shadow may be open */
711 execl(prg
, "SCREEN-LOCK", NULL
);
715 Msg(errno
, "Cannot lock terminal - fork failed");
726 signal(SIGCHLD
, SIG_DFL
);
729 while (((wret
= wait(&wstat
)) != pid
) ||
730 ((wret
== -1) && (errno
== EINTR
))
739 else if (WTERMSIG(wstat
) != 0)
741 fprintf(stderr
, "Lock: %s: Killed by signal: %d%s\n", prg
,
742 WTERMSIG(wstat
), WIFCORESIG(wstat
) ? " (Core dumped)" : "");
745 else if (WEXITSTATUS(wstat
))
747 debug2("Lock: %s: return code %d\n", prg
, WEXITSTATUS(wstat
));
757 debug1("lockterminal: '%s' seems NOT executable, we use our builtin\n", prg
);
761 debug("lockterminal: using buitin.\n");
763 screen_builtin_lck();
766 for (sig
= 1; sig
< NSIG
; sig
++)
768 if (sigs
[sig
] != (sigret_t(*)__P(SIGPROTOARG
)) -1)
769 signal(sig
, sigs
[sig
]);
776 * PAM support by Pablo Averbuj <pablo@averbuj.com>
779 #include <security/pam_appl.h>
781 static int PAM_conv
__P((int, const struct pam_message
**, struct pam_response
**, void *));
784 PAM_conv(num_msg
, msg
, resp
, appdata_ptr
)
786 const struct pam_message
**msg
;
787 struct pam_response
**resp
;
791 struct pam_response
*reply
= NULL
;
793 reply
= malloc(sizeof(struct pam_response
)*num_msg
);
796 #define COPY_STRING(s) (s) ? strdup(s) : NULL
798 for (replies
= 0; replies
< num_msg
; replies
++)
800 switch (msg
[replies
]->msg_style
)
802 case PAM_PROMPT_ECHO_OFF
:
804 reply
[replies
].resp_retcode
= PAM_SUCCESS
;
805 reply
[replies
].resp
= appdata_ptr
? strdup((char *)appdata_ptr
) : 0;
808 /* ignore the informational mesage */
809 /* but first clear out any drek left by malloc */
810 reply
[replies
].resp
= NULL
;
812 case PAM_PROMPT_ECHO_ON
:
813 /* user name given to PAM already */
816 /* unknown or PAM_ERROR_MSG */
825 static struct pam_conv PAM_conversation
= {
833 /* -- original copyright by Luigi Cannelloni 1985 (luigi@faui70.UUCP) -- */
837 char fullname
[100], *cp1
, message
[100 + 100];
839 pam_handle_t
*pamh
= 0;
842 char *pass
, mypass
[16 + 1], salt
[3];
846 pass
= ppp
->pw_passwd
;
847 if (pass
== 0 || *pass
== 0)
849 if ((pass
= getpass("Key: ")))
851 strncpy(mypass
, pass
, sizeof(mypass
) - 1);
852 mypass
[sizeof(mypass
) - 1] = 0;
855 if ((pass
= getpass("Again: ")))
857 if (strcmp(mypass
, pass
))
859 fprintf(stderr
, "Passwords don't match.\007\n");
867 fprintf(stderr
, "Getpass error.\007\n");
872 salt
[0] = 'A' + (int)(time(0) % 26);
873 salt
[1] = 'A' + (int)((time(0) >> 6) % 26);
875 pass
= crypt(mypass
, salt
);
876 pass
= ppp
->pw_passwd
= SaveStr(pass
);
880 debug("screen_builtin_lck looking in gcos field\n");
881 strncpy(fullname
, ppp
->pw_gecos
, sizeof(fullname
) - 9);
882 fullname
[sizeof(fullname
) - 9] = 0;
884 if ((cp1
= index(fullname
, ',')) != NULL
)
886 if ((cp1
= index(fullname
, '&')) != NULL
)
888 strncpy(cp1
, ppp
->pw_name
, 8);
890 if (*cp1
>= 'a' && *cp1
<= 'z')
894 sprintf(message
, "Screen used by %s%s<%s>.\nPassword:\007",
895 fullname
, fullname
[0] ? " " : "", ppp
->pw_name
);
897 /* loop here to wait for correct password */
900 debug("screen_builtin_lck awaiting password\n");
902 if ((cp1
= getpass(message
)) == NULL
)
904 AttacherFinit(SIGARG
);
908 PAM_conversation
.appdata_ptr
= cp1
;
909 pam_error
= pam_start("screen", ppp
->pw_name
, &PAM_conversation
, &pamh
);
910 if (pam_error
!= PAM_SUCCESS
)
911 AttacherFinit(SIGARG
); /* goodbye */
912 pam_error
= pam_authenticate(pamh
, 0);
913 pam_end(pamh
, pam_error
);
914 PAM_conversation
.appdata_ptr
= 0;
915 if (pam_error
== PAM_SUCCESS
)
918 if (!strncmp(crypt(cp1
, pass
), pass
, strlen(pass
)))
921 debug("screen_builtin_lck: NO!!!!!\n");
922 bzero(cp1
, strlen(cp1
));
924 bzero(cp1
, strlen(cp1
));
925 debug("password ok.\n");
932 SendCmdMessage(sty
, match
, av
)
944 i
= FindSocket(&s
, (int *)0, (int *)0, match
);
946 Panic(0, "No screen session found.");
948 Panic(0, "Use -S to specify a session.");
953 if (strlen(sty
) > NAME_MAX
)
956 if (strlen(sty
) > 2 * MAXSTR
- 1)
957 sty
[2 * MAXSTR
- 1] = 0;
958 sprintf(SockPath
+ strlen(SockPath
), "/%s", sty
);
959 if ((s
= MakeClientSocket(1)) == -1)
962 bzero((char *)&m
, sizeof(m
));
963 m
.type
= MSG_COMMAND
;
966 strncpy(m
.m_tty
, attach_tty
, sizeof(m
.m_tty
) - 1);
967 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
971 for (; *av
&& n
< MAXARGS
- 1; ++av
, ++n
)
973 len
= strlen(*av
) + 1;
974 if (p
+ len
>= m
.m
.command
.cmd
+ sizeof(m
.m
.command
.cmd
) - 1)
980 m
.m
.command
.nargs
= n
;
981 strncpy(m
.m
.attach
.auser
, LoginName
, sizeof(m
.m
.attach
.auser
) - 1);
982 m
.m
.command
.auser
[sizeof(m
.m
.command
.auser
) - 1] = 0;
983 m
.protocol_revision
= MSG_REVISION
;
984 strncpy(m
.m
.command
.preselect
, preselect
? preselect
: "", sizeof(m
.m
.command
.preselect
) - 1);
985 m
.m
.command
.preselect
[sizeof(m
.m
.command
.preselect
) - 1] = 0;
986 m
.m
.command
.apid
= getpid();
987 debug1("SendCommandMsg writing '%s'\n", m
.m
.command
.cmd
);
988 if (WriteMessage(s
, &m
))