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 ****************************************************************
25 #include <sys/types.h>
28 #if !defined(NAMEDPIPE)
29 #include <sys/socket.h>
43 # define dirent direct
48 static int CheckPid
__P((int));
49 static void ExecCreate
__P((struct msg
*));
50 static void DoCommandMsg
__P((struct msg
*));
51 #if defined(_SEQUENT_) && !defined(NAMEDPIPE)
52 # define connect sconnect /* _SEQUENT_ has braindamaged connect */
53 static int sconnect
__P((int, struct sockaddr
*, int));
55 static void FinishAttach
__P((struct msg
*));
56 static void AskPassword
__P((struct msg
*));
59 extern char *RcFileName
, *extra_incap
, *extra_outcap
;
60 extern int ServerSocket
, real_uid
, real_gid
, eff_uid
, eff_gid
;
61 extern int dflag
, iflag
, rflag
, lsflag
, quietflag
, wipeflag
, xflag
;
62 extern char *attach_tty
, *LoginName
, HostName
[];
63 extern struct display
*display
, *displays
;
64 extern struct win
*fore
, *wtab
[], *console_window
, *windows
;
65 extern struct layer
*flayer
;
66 extern struct layout
*layout_attach
, *layout_last
, layout_last_marker
;
67 extern struct NewWindow nwin_undef
;
72 extern char *getenv();
74 extern char SockPath
[];
75 extern struct event serv_read
;
77 extern struct comm comms
[];
80 # define SOCKMODE (S_IWRITE | S_IREAD | (displays ? S_IEXEC : 0) | (multi ? 1 : 0))
82 # define SOCKMODE (S_IWRITE | S_IREAD | (displays ? S_IEXEC : 0))
87 * Socket directory manager
89 * fdp: pointer to store the first good socket.
90 * nfoundp: pointer to store the number of sockets found matching.
91 * notherp: pointer to store the number of sockets not matching.
92 * match: string to match socket name.
94 * The socket directory must be in SockPath!
95 * The global variables LoginName, multi, rflag, xflag, dflag,
96 * quietflag, SockPath are used.
98 * The first good socket is stored in fdp and its name is
99 * appended to SockPath.
100 * If none exists or fdp is NULL SockPath is not changed.
102 * Returns: number of good sockets.
107 FindSocket(fdp
, nfoundp
, notherp
, match
)
109 int *nfoundp
, *notherp
;
119 int firsts
= -1, sockfd
;
121 int nfound
= 0, ngood
= 0, ndead
= 0, nwipe
= 0, npriv
= 0;
127 } *slist
, **slisttail
, *sent
, *nsent
;
131 matchlen
= strlen(match
);
133 if (matchlen
> NAME_MAX
)
139 * SockPath contains the socket directory.
140 * At the end of FindSocket the socket name will be appended to it.
141 * Thus FindSocket() can only be called once!
143 sdirlen
= strlen(SockPath
);
150 if ((dirp
= opendir(SockPath
)) == 0)
151 Panic(errno
, "Cannot opendir %s", SockPath
);
155 while ((dp
= readdir(dirp
)))
158 debug1("- %s\n", name
);
159 if (*name
== 0 || *name
== '.' || strlen(name
) > 2*MAXSTR
)
164 /* if we don't want to match digits. Skip them */
165 if ((*match
<= '0' || *match
> '9') && (*n
> '0' && *n
<= '9'))
167 while (*n
>= '0' && *n
<= '9')
172 /* the tty prefix is optional */
173 if (strncmp(match
, "tty", 3) && strncmp(n
, "tty", 3) == 0)
175 if (strncmp(match
, n
, matchlen
))
177 debug1(" -> matched %s\n", match
);
179 sprintf(SockPath
+ sdirlen
, "/%s", name
);
181 debug1("stat %s\n", SockPath
);
183 debug2("uid = %d, gid = %d\n", getuid(), getgid());
184 debug2("euid = %d, egid = %d\n", geteuid(), getegid());
185 if (stat(SockPath
, &st
))
187 debug1("errno = %d\n", errno
);
191 #ifndef SOCK_NOT_IN_FS
194 debug("S_ISFIFO?\n");
195 if (!S_ISFIFO(st
.st_mode
))
200 debug("S_ISSOCK?\n");
201 if (!S_ISSOCK(st
.st_mode
))
207 debug2("st.st_uid = %d, real_uid = %d\n", st
.st_uid
, real_uid
);
208 if ((int)st
.st_uid
!= real_uid
)
210 mode
= (int)st
.st_mode
& 0777;
211 debug1(" has mode 0%03o\n", mode
);
213 if (multi
&& ((mode
& 0677) != 0601))
215 debug(" is not a MULTI-USER session");
216 if (strcmp(multi
, LoginName
))
218 debug(" and we are in a foreign directory.\n");
223 debug(", but it is our own session.\n");
227 debug(" store it.\n");
228 if ((sent
= (struct sent
*)malloc(sizeof(struct sent
))) == 0)
231 sent
->name
= SaveStr(name
);
234 slisttail
= &sent
->next
;
236 sockfd
= MakeClientSocket(0);
238 /* MakeClientSocket sets ids back to eff */
244 debug2(" MakeClientSocket failed, unreachable? %d %d\n",
247 #ifndef SOCKDIR_IS_LOCAL_TO_HOST
248 /* Unreachable - it is dead if we detect that it's local
249 * or we specified a match
251 n
= name
+ strlen(name
) - 1;
252 while (n
!= name
&& *n
!= '.')
254 if (matchlen
== 0 && !(*n
== '.' && n
[1] && strncmp(HostName
, n
+ 1, strlen(n
+ 1)) == 0))
256 npriv
++; /* a good socket that was not for us */
264 if (unlink(SockPath
) == 0)
274 /* Shall we connect ? */
275 debug2(" connecting: mode=%03o, rflag=%d, ", mode
, rflag
);
276 debug2("xflag=%d, dflag=%d ?\n", xflag
, dflag
);
279 * mode 600: socket is detached.
280 * mode 700: socket is attached.
281 * xflag implies rflag here.
283 * fail, when socket mode mode is not 600 or 700
284 * fail, when we want to detach w/o reattach, but it already is detached.
285 * fail, when we only want to attach, but mode 700 and not xflag.
286 * fail, if none of dflag, rflag, xflag is set.
288 if ((mode
!= 0700 && mode
!= 0600) ||
289 (dflag
&& !rflag
&& !xflag
&& mode
== 0600) ||
290 (!dflag
&& rflag
&& mode
== 0700 && !xflag
) ||
291 (!dflag
&& !rflag
&& !xflag
))
295 npriv
++; /* a good socket that was not for us */
299 if (fdp
&& firsts
== -1)
307 debug(" discarded.\n");
311 (void)closedir(dirp
);
312 if (nfound
&& (lsflag
|| ngood
!= 1) && !quietflag
)
317 Msg(0, nfound
> 1 ? "There are screens on:" : "There is a screen on:");
320 Msg(0, nfound
> 1 ? "There are several screens on:" : "There is a suitable screen on:");
323 Msg(0, "There are several suitable screens on:");
326 for (sent
= slist
; sent
; sent
= sent
->next
)
331 printf("\t%s\t(Attached)\n", sent
->name
);
334 printf("\t%s\t(Detached)\n", sent
->name
);
338 printf("\t%s\t(Multi, attached)\n", sent
->name
);
341 printf("\t%s\t(Multi, detached)\n", sent
->name
);
345 /* No trigraphs here! */
346 printf("\t%s\t(Dead ?%c?)\n", sent
->name
, '?');
349 printf("\t%s\t(Removed)\n", sent
->name
);
352 printf("\t%s\t(Remote or dead)\n", sent
->name
);
355 printf("\t%s\t(Private)\n", sent
->name
);
360 if (ndead
&& !quietflag
)
362 char *m
= "Remove dead screens with 'screen -wipe'.";
364 Msg(0, "%d socket%s wiped out.", nwipe
, nwipe
> 1 ? "s" : "");
366 Msg(0, m
, ndead
> 1 ? "s" : "", ndead
> 1 ? "" : "es"); /* other args for nethack */
370 sprintf(SockPath
+ sdirlen
, "/%s", firstn
);
374 SockPath
[sdirlen
] = 0;
375 for (sent
= slist
; sent
; sent
= nsent
)
388 *nfoundp
= nfound
- nwipe
;
395 ** Socket/pipe create routines
411 if ((s
= open(SockPath
, O_WRONLY
| O_NONBLOCK
)) >= 0)
413 debug("huii, my fifo already exists??\n");
416 Kill(D_userpid
, SIG_BYE
);
419 Msg(0, "There is already a screen running on %s.", Filename(SockPath
));
420 if (stat(SockPath
, &st
) == -1)
421 Panic(errno
, "stat");
422 if ((int)st
.st_uid
!= real_uid
)
423 Panic(0, "Unfortunatelly you are not its owner.");
424 if ((st
.st_mode
& 0700) == 0600)
425 Panic(0, "To resume it, use \"screen -r\"");
427 Panic(0, "It is not detached.");
431 (void) unlink(SockPath
);
432 if (mkfifo(SockPath
, SOCKMODE
))
433 Panic(0, "mkfifo %s failed", SockPath
);
435 if ((s
= open(SockPath
, O_RDWR
| O_NONBLOCK
, 0)) < 0)
437 if ((s
= open(SockPath
, O_RDONLY
| O_NONBLOCK
, 0)) < 0)
439 Panic(errno
, "open fifo %s", SockPath
);
443 # else /* !USE_SETEUID */
444 if (UserContext() > 0)
446 (void) unlink(SockPath
);
447 UserReturn(mkfifo(SockPath
, SOCKMODE
));
450 Panic(0, "mkfifo %s failed", SockPath
);
452 if ((s
= secopen(SockPath
, O_RDWR
| O_NONBLOCK
, 0)) < 0)
454 if ((s
= secopen(SockPath
, O_RDONLY
| O_NONBLOCK
, 0)) < 0)
456 Panic(errno
, "open fifo %s", SockPath
);
458 # endif /* !USE_SETEUID */
463 MakeClientSocket(err
)
468 if ((s
= secopen(SockPath
, O_WRONLY
| O_NONBLOCK
, 0)) >= 0)
470 (void) fcntl(s
, F_SETFL
, 0);
474 Msg(errno
, "%s", SockPath
);
475 debug2("MakeClientSocket() open %s failed (%d)\n", SockPath
, errno
);
480 #else /* NAMEDPIPE */
487 struct sockaddr_un a
;
490 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
491 Panic(errno
, "socket");
492 a
.sun_family
= AF_UNIX
;
493 strncpy(a
.sun_path
, SockPath
, sizeof(a
.sun_path
));
494 a
.sun_path
[sizeof(a
.sun_path
) - 1] = 0;
499 if (connect(s
, (struct sockaddr
*) &a
, strlen(SockPath
) + 2) != -1)
501 debug("oooooh! socket already is alive!\n");
504 Kill(D_userpid
, SIG_BYE
);
506 * oh, well. nobody receives that return code. papa
511 Msg(0, "There is already a screen running on %s.", Filename(SockPath
));
512 if (stat(SockPath
, &st
) == -1)
513 Panic(errno
, "stat");
514 if (st
.st_uid
!= real_uid
)
515 Panic(0, "Unfortunatelly you are not its owner.");
516 if ((st
.st_mode
& 0700) == 0600)
517 Panic(0, "To resume it, use \"screen -r\"");
519 Panic(0, "It is not detached.");
522 #if defined(m88k) || defined(sysV68)
523 close(s
); /* we get bind: Invalid argument if this is not done */
524 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
525 Panic(errno
, "reopen socket");
527 (void) unlink(SockPath
);
528 if (bind(s
, (struct sockaddr
*) & a
, strlen(SockPath
) + 2) == -1)
529 Panic(errno
, "bind (%s)", SockPath
);
530 #ifdef SOCK_NOT_IN_FS
533 if ((f
= secopen(SockPath
, O_RDWR
| O_CREAT
, SOCKMODE
)) < 0)
534 Panic(errno
, "shadow socket open");
538 chmod(SockPath
, SOCKMODE
);
540 chown(SockPath
, real_uid
, real_gid
);
542 #endif /* SOCK_NOT_IN_FS */
543 if (listen(s
, 5) == -1)
544 Panic(errno
, "listen");
546 fcntl(s
, F_SETOWN
, getpid());
547 debug1("Serversocket owned by %d\n", fcntl(s
, F_GETOWN
, 0));
548 # endif /* F_SETOWN */
557 MakeClientSocket(err
)
561 struct sockaddr_un a
;
563 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
564 Panic(errno
, "socket");
565 a
.sun_family
= AF_UNIX
;
566 strncpy(a
.sun_path
, SockPath
, sizeof(a
.sun_path
));
567 a
.sun_path
[sizeof(a
.sun_path
) - 1] = 0;
572 if (access(SockPath
, W_OK
))
575 Msg(errno
, "%s", SockPath
);
576 debug2("MakeClientSocket: access(%s): %d.\n", SockPath
, errno
);
581 if (connect(s
, (struct sockaddr
*) &a
, strlen(SockPath
) + 2) == -1)
584 Msg(errno
, "%s: connect", SockPath
);
585 debug("MakeClientSocket: connect failed.\n");
595 #endif /* NAMEDPIPE */
600 ** Message send and receive routines
605 SendCreateMsg(sty
, nwin
)
607 struct NewWindow
*nwin
;
613 char **av
= nwin
->args
;
616 if (strlen(sty
) > NAME_MAX
)
619 if (strlen(sty
) > 2 * MAXSTR
- 1)
620 sty
[2 * MAXSTR
- 1] = 0;
621 sprintf(SockPath
+ strlen(SockPath
), "/%s", sty
);
622 if ((s
= MakeClientSocket(1)) == -1)
624 debug1("SendCreateMsg() to '%s'\n", SockPath
);
625 bzero((char *)&m
, sizeof(m
));
627 strncpy(m
.m_tty
, attach_tty
, sizeof(m
.m_tty
) - 1);
628 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
631 if (nwin
->args
!= nwin_undef
.args
)
632 for (av
= nwin
->args
; *av
&& n
< MAXARGS
- 1; ++av
, ++n
)
634 len
= strlen(*av
) + 1;
635 if (p
+ len
>= m
.m
.create
.line
+ sizeof(m
.m
.create
.line
) - 1)
640 if (nwin
->aka
!= nwin_undef
.aka
&& p
+ strlen(nwin
->aka
) + 1 < m
.m
.create
.line
+ sizeof(m
.m
.create
.line
))
641 strcpy(p
, nwin
->aka
);
644 m
.m
.create
.nargs
= n
;
645 m
.m
.create
.aflag
= nwin
->aflag
;
646 m
.m
.create
.flowflag
= nwin
->flowflag
;
647 m
.m
.create
.lflag
= nwin
->lflag
;
648 m
.m
.create
.hheight
= nwin
->histheight
;
649 if (getcwd(m
.m
.create
.dir
, sizeof(m
.m
.create
.dir
)) == 0)
651 Msg(errno
, "getcwd");
654 if (nwin
->term
!= nwin_undef
.term
)
655 strncpy(m
.m
.create
.screenterm
, nwin
->term
, 19);
656 m
.m
.create
.screenterm
[19] = '\0';
657 m
.protocol_revision
= MSG_REVISION
;
658 debug1("SendCreateMsg writing '%s'\n", m
.m
.create
.line
);
659 if (write(s
, (char *) &m
, sizeof m
) != sizeof m
)
665 SendErrorMsg(tty
, buf
)
671 strncpy(m
.m
.message
, buf
, sizeof(m
.m
.message
) - 1);
672 m
.m
.message
[sizeof(m
.m
.message
) - 1] = 0;
673 s
= MakeClientSocket(0);
677 strncpy(m
.m_tty
, tty
, sizeof(m
.m_tty
) - 1);
678 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
679 m
.protocol_revision
= MSG_REVISION
;
680 debug1("SendErrorMsg(): writing to '%s'\n", SockPath
);
681 (void) write(s
, (char *) &m
, sizeof m
);
690 struct NewWindow nwin
;
693 register char **pp
= args
, *p
= mp
->m
.create
.line
;
696 n
= mp
->m
.create
.nargs
;
699 /* ugly hack alert... should be done by the frontend! */
706 if (IsNumColon(p
, 10, buf
, sizeof(buf
)))
711 if (num
< 0 || num
> MAXWIN
- 1)
728 nwin
.aflag
= mp
->m
.create
.aflag
;
729 nwin
.flowflag
= mp
->m
.create
.flowflag
;
730 if (*mp
->m
.create
.dir
)
731 nwin
.dir
= mp
->m
.create
.dir
;
732 nwin
.lflag
= mp
->m
.create
.lflag
;
733 nwin
.histheight
= mp
->m
.create
.hheight
;
734 if (*mp
->m
.create
.screenterm
)
735 nwin
.term
= mp
->m
.create
.screenterm
;
743 debug1("Checking pid %d\n", pid
);
746 if (eff_uid
== real_uid
)
748 if (UserContext() > 0)
749 UserReturn(kill(pid
, 0));
755 * From: "F. K. Bruner" <napalm@ugcs.caltech.edu>
756 * From: "Dan Egnor" <egnor@oracorp.com> Tue Aug 10 06:56:45 1993
757 * The problem is that under HPUX (and possibly other systems too) there are
758 * two equivalent device files for each pty/tty device:
759 * /dev/ttyxx == /dev/pty/ttyxx
760 * /dev/ptyxx == /dev/ptym/ptyxx
761 * I didn't look into the exact specifics, but I've run across this problem
762 * before: Even if you open /dev/ttyxx as fds 0 1 & 2 for a process, if that
763 * process calls the system to determine its tty, it'll get /dev/pty/ttyxx.
765 * Earlier versions seemed to work -- wonder what they did.
771 if (strlen(s1
) > 5) s1
+= strlen(s1
) - 5;
772 if (strlen(s2
) > 5) s2
+= strlen(s2
) - 5;
773 return strcmp(s1
, s2
);
775 # define TTYCMP(a, b) ttycmp(a, b)
777 # define TTYCMP(a, b) strcmp(a, b)
786 int ns
= ServerSocket
;
790 struct display
*next
;
792 struct display
*olddisplays
= displays
;
796 debug("Ha, there was someone knocking on my fifo??\n");
797 if (fcntl(ServerSocket
, F_SETFL
, 0) == -1)
798 Panic(errno
, "BLOCK fcntl");
802 struct sockaddr_un a
;
808 debug("Ha, there was someone knocking on my socket??\n");
809 if ((ns
= accept(ns
, (struct sockaddr
*) &a
, (void *)&len
)) < 0)
811 Msg(errno
, "accept");
817 bzero(&msg
, sizeof(msg
));
822 msg
.msg_controllen
= sizeof(control
);
823 msg
.msg_control
= &control
;
826 len
= recvmsg(ns
, &msg
, 0);
827 if (len
< 0 && errno
== EINTR
)
835 if (msg
.msg_controllen
)
837 struct cmsghdr
*cmsg
;
838 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
842 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
)
844 cp
= (char *)CMSG_DATA(cmsg
);
846 while(cl
>= CMSG_LEN(sizeof(int)))
849 bcopy(cp
, &passedfd
, sizeof(int));
850 if (recvfd
>= 0 && passedfd
!= recv
)
853 cl
-= CMSG_LEN(sizeof(int));
866 len
= read(ns
, p
, left
);
867 if (len
< 0 && errno
== EINTR
)
877 /* Reopen pipe to prevent EOFs at the select() call */
879 if ((ServerSocket
= secopen(SockPath
, O_RDONLY
| O_NONBLOCK
, 0)) < 0)
880 Panic(errno
, "reopen fifo %s", SockPath
);
882 serv_read
.fd
= ServerSocket
;
898 if (left
!= sizeof(m
))
899 Msg(0, "Message %d of %d bytes too small", left
, (int)sizeof(m
));
901 debug("No data on socket.\n");
904 if (m
.protocol_revision
!= MSG_REVISION
)
908 Msg(0, "Invalid message (magic 0x%08x).", m
.protocol_revision
);
912 debug2("*** RecMsg: type %d tty %s\n", m
.type
, m
.m_tty
);
913 if (m
.type
!= MSG_ATTACH
&& recvfd
!= -1)
919 for (display
= displays
; display
; display
= display
->d_next
)
920 if (TTYCMP(D_usertty
, m
.m_tty
) == 0)
922 debug2("display: %s display %sfound\n", m
.m_tty
, display
? "" : "not ");
926 for (wi
= windows
; wi
; wi
= wi
->w_next
)
927 if (!TTYCMP(m
.m_tty
, wi
->w_tty
))
929 /* XXX: hmmm, rework this? */
930 display
= wi
->w_layer
.l_cvlist
? wi
->w_layer
.l_cvlist
->c_display
: 0;
931 debug2("but window %s %sfound.\n", m
.m_tty
, display
? "" :
937 /* Remove the status to prevent garbage on the screen */
938 if (display
&& D_status
)
941 if (display
&& !D_tcinited
&& m
.type
!= MSG_HANGUP
)
945 return; /* ignore messages for bad displays */
952 CheckScreenSize(1); /* Change fore */
956 * the window that issued the create message need not be an active
957 * window. Then we create the window without having a display.
958 * Resulting in another inactive window.
960 * Currently we enforce that at least one display exists. But why?
967 if (display
&& D_userpid
!= 0 && kill(D_userpid
, 0) == 0)
968 break; /* Intruder Alert */
969 debug2("RecMsg: apid=%d,was %d\n", m
.m
.attach
.apid
, display
? D_userpid
: 0);
973 if (CheckPid(m
.m
.attach
.apid
))
975 Msg(0, "Attach attempt with bad pid(%d)!", m
.m
.attach
.apid
);
983 myttyname
= ttyname(i
);
984 if (myttyname
== 0 || strcmp(myttyname
, m
.m_tty
))
986 Msg(errno
, "Attach: passed fd does not match tty: %s - %s!", m
.m_tty
, myttyname
? myttyname
: "NULL");
988 Kill(m
.m
.attach
.apid
, SIG_BYE
);
992 else if ((i
= secopen(m
.m_tty
, O_RDWR
| O_NONBLOCK
, 0)) < 0)
994 Msg(errno
, "Attach: Could not open %s!", m
.m_tty
);
995 Kill(m
.m
.attach
.apid
, SIG_BYE
);
999 Kill(m
.m
.attach
.apid
, SIGCONT
);
1002 #if defined(ultrix) || defined(pyr) || defined(NeXT)
1003 brktty(i
); /* for some strange reason this must be done */
1008 write(i
, "Attaching from inside of screen?\n", 33);
1010 Kill(m
.m
.attach
.apid
, SIG_BYE
);
1011 Msg(0, "Attach msg ignored: coming from inside.");
1016 if (strcmp(m
.m
.attach
.auser
, LoginName
))
1017 if (*FindUserPtr(m
.m
.attach
.auser
) == 0)
1019 write(i
, "Access to session denied.\n", 26);
1021 Kill(m
.m
.attach
.apid
, SIG_BYE
);
1022 Msg(0, "Attach: access denied for user %s.", m
.m
.attach
.auser
);
1027 debug2("RecMsg: apid %d is o.k. and we just opened '%s'\n", m
.m
.attach
.apid
, m
.m_tty
);
1031 write(i
, "Screen session in use.\n", 23);
1033 Kill(m
.m
.attach
.apid
, SIG_BYE
);
1038 /* create new display */
1040 if (MakeDisplay(m
.m
.attach
.auser
, m
.m_tty
, m
.m
.attach
.envterm
, i
, m
.m
.attach
.apid
, &Mode
) == 0)
1042 write(i
, "Could not make display.\n", 24);
1044 Msg(0, "Attach: could not make display for user %s", m
.m
.attach
.auser
);
1045 Kill(m
.m
.attach
.apid
, SIG_BYE
);
1050 D_encoding
= m
.m
.attach
.encoding
== 1 ? UTF8
: m
.m
.attach
.encoding
? m
.m
.attach
.encoding
- 1 : 0;
1052 D_encoding
= m
.m
.attach
.encoding
? m
.m
.attach
.encoding
- 1 : 0;
1054 if (D_encoding
< 0 || !EncodingName(D_encoding
))
1057 /* turn off iflag on a multi-attach... */
1058 if (iflag
&& olddisplays
)
1061 #if defined(TERMIO) || defined(POSIX)
1062 olddisplays
->d_NewMode
.tio
.c_cc
[VINTR
] = VDISABLE
;
1063 olddisplays
->d_NewMode
.tio
.c_lflag
&= ~ISIG
;
1064 #else /* TERMIO || POSIX */
1065 olddisplays
->d_NewMode
.m_tchars
.t_intrc
= -1;
1066 #endif /* TERMIO || POSIX */
1067 SetTTY(olddisplays
->d_userfd
, &olddisplays
->d_NewMode
);
1069 SetMode(&D_OldMode
, &D_NewMode
, D_flow
, iflag
);
1070 SetTTY(D_userfd
, &D_NewMode
);
1071 if (fcntl(D_userfd
, F_SETFL
, FNBLOCK
))
1072 Msg(errno
, "Warning: NBLOCK fcntl failed");
1075 if (D_user
->u_password
&& *D_user
->u_password
)
1082 Msg(0, "%s", m
.m
.message
);
1085 if (!wi
) /* ignore hangups from inside */
1088 #ifdef REMOTE_DETACH
1091 case MSG_POW_DETACH
:
1092 # endif /* POW_DETACH */
1093 for (display
= displays
; display
; display
= next
)
1095 next
= display
->d_next
;
1097 if (m
.type
== MSG_POW_DETACH
)
1098 Detach(D_REMOTE_POWER
);
1100 # endif /* POW_DETACH */
1101 if (m
.type
== MSG_DETACH
)
1110 Msg(0, "Invalid message (type %d).", m
.type
);
1114 #if defined(_SEQUENT_) && !defined(NAMEDPIPE)
1117 * sequent_ptx socket emulation must have mode 000 on the socket!
1120 sconnect(s
, sapp
, len
)
1122 struct sockaddr
*sapp
;
1124 register struct sockaddr_un
*sap
;
1128 sap
= (struct sockaddr_un
*)sapp
;
1129 if (stat(sap
->sun_path
, &st
))
1131 chmod(sap
->sun_path
, 0);
1132 x
= connect(s
, (struct sockaddr
*) sap
, len
);
1133 chmod(sap
->sun_path
, st
.st_mode
);
1140 * Set the mode bits of the socket to the current status
1145 int r
, euid
= geteuid();
1146 if (euid
!= real_uid
)
1148 if (UserContext() <= 0)
1149 return UserStatus();
1151 r
= chmod(SockPath
, SOCKMODE
);
1153 * Sockets usually reside in the /tmp/ area, where sysadmin scripts
1154 * may be happy to remove old files. We manually prevent the socket
1155 * from becoming old. (chmod does not touch mtime).
1157 (void)utimes(SockPath
, NULL
);
1159 if (euid
!= real_uid
)
1165 * Try to recreate the socket/pipe
1170 close(ServerSocket
);
1171 if ((int)geteuid() != real_uid
)
1173 if (UserContext() > 0)
1174 UserReturn(unlink(SockPath
));
1178 (void) unlink(SockPath
);
1180 if ((ServerSocket
= MakeServerSocket()) < 0)
1183 serv_read
.fd
= ServerSocket
;
1202 #if defined(pyr) || defined(xelos) || defined(sequent)
1204 * Kludge for systems with braindamaged termcap routines,
1205 * which evaluate $TERMCAP, regardless weather it describes
1206 * the correct terminal type or not.
1208 debug("unsetenv(TERMCAP) in case of a different terminal");
1209 unsetenv("TERMCAP");
1213 * We reboot our Terminal Emulator. Forget all we knew about
1214 * the old terminal, reread the termcap entries in .screenrc
1215 * (and nothing more from .screenrc is read. Mainly because
1216 * I did not check, weather a full reinit is safe. jw)
1217 * and /etc/screenrc, and initialise anew.
1223 extra_incap
= extra_outcap
= 0;
1224 debug2("Message says size (%dx%d)\n", m
->m
.attach
.columns
, m
->m
.attach
.lines
);
1226 # ifdef ALLOW_SYSSCREENRC
1227 if ((p
= getenv("SYSSCREENRC")))
1231 StartRc(ETCSCREENRC
, 1);
1233 StartRc(RcFileName
, 1);
1234 if (InitTermcap(m
->m
.attach
.columns
, m
->m
.attach
.lines
))
1240 MakeDefaultCanvas();
1241 InitTerm(m
->m
.attach
.adaptflag
); /* write init string on fd */
1242 if (displays
->d_next
== 0)
1244 signal(SIGHUP
, SigHup
);
1245 if (m
->m
.attach
.esc
!= -1 && m
->m
.attach
.meta_esc
!= -1)
1247 D_user
->u_Esc
= m
->m
.attach
.esc
;
1248 D_user
->u_MetaEsc
= m
->m
.attach
.meta_esc
;
1253 * we set the Utmp slots again, if we were detached normally
1254 * and if we were detached by ^Z.
1255 * don't log zomies back in!
1258 if (displays
->d_next
== 0)
1259 for (wi
= windows
; wi
; wi
= wi
->w_next
)
1260 if (wi
->w_ptyfd
>= 0 && wi
->w_slot
!= (slot_t
) -1)
1267 struct layout
*lay
= layout_attach
;
1268 if (lay
== &layout_last_marker
)
1272 LoadLayout(lay
, &D_canvas
);
1273 SetCanvasWindow(D_forecv
, 0);
1277 * there may be a window that we remember from last detach:
1279 debug1("D_user->u_detachwin = %d\n", D_user
->u_detachwin
);
1280 if (D_user
->u_detachwin
>= 0)
1281 fore
= wtab
[D_user
->u_detachwin
];
1285 /* Wayne wants us to restore the other window too. */
1286 if (D_user
->u_detachotherwin
>= 0)
1287 D_other
= wtab
[D_user
->u_detachotherwin
];
1290 if (*m
->m
.attach
.preselect
)
1292 if (!strcmp(m
->m
.attach
.preselect
, "="))
1294 else if (!strcmp(m
->m
.attach
.preselect
, "-"))
1299 else if (!strcmp(m
->m
.attach
.preselect
, "+"))
1301 struct action newscreen
;
1303 newscreen
.nr
= RC_SCREEN
;
1304 newscreen
.args
= &na
;
1305 DoAction(&newscreen
, -1);
1308 fore
= FindNiceWindow(fore
, m
->m
.attach
.preselect
);
1311 fore
= FindNiceWindow(fore
, 0);
1313 SetForeWindow(fore
);
1314 else if (!noshowwin
)
1317 if (!AclCheckPermCmd(D_user
, ACL_EXEC
, &comms
[RC_WINDOWLIST
]))
1320 flayer
= D_forecv
->c_layer
;
1321 display_wlist(1, WLIST_NUM
, (char *)0);
1327 if (!D_fore
&& !noshowwin
)
1329 if (displays
->d_next
== 0 && console_window
)
1331 if (TtyGrabConsole(console_window
->w_ptyfd
, 1, "reattach") == 0)
1332 Msg(0, "console %s is on window %d", HostName
, console_window
->w_number
);
1334 debug("activated...\n");
1336 # if defined(DEBUG) && defined(SIG_NODEBUG)
1340 debug1("Attacher %d must not debug, as we have debug off.\n", pid
);
1341 kill(pid
, SIG_NODEBUG
);
1343 # endif /* SIG_NODEBUG */
1348 static void PasswordProcessInput
__P((char *, int));
1360 struct pwdata
*pwdata
;
1362 pwdata
= (struct pwdata
*)malloc(sizeof(struct pwdata
));
1367 D_processinputdata
= (char *)pwdata
;
1368 D_processinput
= PasswordProcessInput
;
1369 AddStr("Screen password: ");
1373 PasswordProcessInput(ibuf
, ilen
)
1377 struct pwdata
*pwdata
;
1380 int pid
= D_userpid
;
1382 pwdata
= (struct pwdata
*)D_processinputdata
;
1386 c
= *(unsigned char *)ibuf
++;
1387 if (c
== '\r' || c
== '\n')
1389 up
= D_user
->u_password
;
1391 if (strncmp(crypt(pwdata
->buf
, up
), up
, strlen(up
)))
1393 /* uh oh, user failed */
1394 bzero(pwdata
->buf
, sizeof(pwdata
->buf
));
1395 AddStr("\r\nPassword incorrect.\r\n");
1396 D_processinputdata
= 0; /* otherwise freed by FreeDis */
1398 Msg(0, "Illegal reattach attempt from terminal %s.", pwdata
->m
.m_tty
);
1403 /* great, pw matched, all is fine */
1404 bzero(pwdata
->buf
, sizeof(pwdata
->buf
));
1406 D_processinputdata
= 0;
1407 D_processinput
= ProcessInput
;
1408 FinishAttach(&pwdata
->m
);
1419 if (c
== '\b' || c
== 0177)
1430 if (l
< (int)sizeof(pwdata
->buf
) - 1)
1431 pwdata
->buf
[l
++] = c
;
1441 char *args
[MAXARGS
];
1444 register char **pp
= args
, *p
= mp
->m
.command
.cmd
;
1445 struct acluser
*user
;
1447 extern struct acluser
*EffectiveAclUser
; /* acls.c */
1449 extern struct acluser
*users
; /* acls.c */
1453 n
= mp
->m
.command
.nargs
;
1454 if (n
> MAXARGS
- 1)
1464 user
= *FindUserPtr(mp
->m
.attach
.auser
);
1467 Msg(0, "Unknown user %s tried to send a command!", mp
->m
.attach
.auser
);
1474 if (user
->u_password
&& *user
->u_password
)
1476 Msg(0, "User %s has a password, cannot use -X option.", mp
->m
.attach
.auser
);
1481 for (display
= displays
; display
; display
= display
->d_next
)
1484 for (fore
= windows
; fore
; fore
= fore
->w_next
)
1485 if (!TTYCMP(mp
->m_tty
, fore
->w_tty
))
1488 display
= fore
->w_layer
.l_cvlist
? fore
->w_layer
.l_cvlist
->c_display
: 0;
1492 display
= displays
; /* sigh */
1493 if (*mp
->m
.command
.preselect
)
1496 if (strcmp(mp
->m
.command
.preselect
, "-"))
1497 i
= WindowByNoN(mp
->m
.command
.preselect
);
1498 fore
= i
>= 0 ? wtab
[i
] : 0;
1502 if (display
&& D_user
== user
)
1503 fore
= Layer2Window(display
->d_forecv
->c_layer
);
1506 fore
= user
->u_detachwin
>= 0 ? wtab
[user
->u_detachwin
] : 0;
1507 fore
= FindNiceWindow(fore
, 0);
1511 EffectiveAclUser
= user
;
1515 char *oldrcname
= rc_name
;
1517 debug3("Running command on display %x window %x (%d)\n", display
, fore
, fore
? fore
->w_number
: -1);
1518 flayer
= fore
? &fore
->w_layer
: 0;
1519 if (fore
&& fore
->w_savelayer
&& (fore
->w_blocked
|| fore
->w_savelayer
->l_cvlist
== 0))
1520 flayer
= fore
->w_savelayer
;
1521 DoCommand(args
, argl
);
1522 rc_name
= oldrcname
;
1525 EffectiveAclUser
= 0;
1532 SendAttachMsg(s
, m
, fd
)
1540 char buf
[CMSG_SPACE(sizeof(int))];
1541 struct cmsghdr
*cmsg
;
1543 iov
.iov_base
= (char *)m
;
1544 iov
.iov_len
= sizeof(*m
);
1545 bzero(&msg
, sizeof(msg
));
1547 msg
.msg_namelen
= 0;
1550 msg
.msg_control
= buf
;
1551 msg
.msg_controllen
= sizeof(buf
);
1552 cmsg
= CMSG_FIRSTHDR(&msg
);
1553 cmsg
->cmsg_level
= SOL_SOCKET
;
1554 cmsg
->cmsg_type
= SCM_RIGHTS
;
1555 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
1556 bcopy(&fd
, CMSG_DATA(cmsg
), sizeof(int));
1557 msg
.msg_controllen
= cmsg
->cmsg_len
;
1560 r
= sendmsg(s
, &msg
, 0);
1561 if (r
== -1 && errno
== EINTR
)