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 FinishDetach
__P((struct msg
*));
57 static void AskPassword
__P((struct msg
*));
60 extern char *RcFileName
, *extra_incap
, *extra_outcap
;
61 extern int ServerSocket
, real_uid
, real_gid
, eff_uid
, eff_gid
;
62 extern int dflag
, iflag
, rflag
, lsflag
, quietflag
, wipeflag
, xflag
;
63 extern char *attach_tty
, *LoginName
, HostName
[];
64 extern struct display
*display
, *displays
;
65 extern struct win
*fore
, *wtab
[], *console_window
, *windows
;
66 extern struct layer
*flayer
;
67 extern struct layout
*layout_attach
, *layout_last
, layout_last_marker
;
68 extern struct NewWindow nwin_undef
;
73 extern char *getenv();
75 extern char SockPath
[];
76 extern struct event serv_read
;
78 extern struct comm comms
[];
81 # define SOCKMODE (S_IWRITE | S_IREAD | (displays ? S_IEXEC : 0) | (multi ? 1 : 0))
83 # define SOCKMODE (S_IWRITE | S_IREAD | (displays ? S_IEXEC : 0))
88 * Socket directory manager
90 * fdp: pointer to store the first good socket.
91 * nfoundp: pointer to store the number of sockets found matching.
92 * notherp: pointer to store the number of sockets not matching.
93 * match: string to match socket name.
95 * The socket directory must be in SockPath!
96 * The global variables LoginName, multi, rflag, xflag, dflag,
97 * quietflag, SockPath are used.
99 * The first good socket is stored in fdp and its name is
100 * appended to SockPath.
101 * If none exists or fdp is NULL SockPath is not changed.
103 * Returns: number of good sockets.
108 FindSocket(fdp
, nfoundp
, notherp
, match
)
110 int *nfoundp
, *notherp
;
120 int firsts
= -1, sockfd
;
122 int nfound
= 0, ngood
= 0, ndead
= 0, nwipe
= 0, npriv
= 0;
129 } *slist
, **slisttail
, *sent
, *nsent
;
133 matchlen
= strlen(match
);
135 if (matchlen
> NAME_MAX
)
141 * SockPath contains the socket directory.
142 * At the end of FindSocket the socket name will be appended to it.
143 * Thus FindSocket() can only be called once!
145 sdirlen
= strlen(SockPath
);
152 if ((dirp
= opendir(SockPath
)) == 0)
153 Panic(errno
, "Cannot opendir %s", SockPath
);
157 while ((dp
= readdir(dirp
)))
161 debug1("- %s\n", name
);
162 if (*name
== 0 || *name
== '.' || strlen(name
) > 2*MAXSTR
)
167 /* if we don't want to match digits. Skip them */
168 if ((*match
<= '0' || *match
> '9') && (*n
> '0' && *n
<= '9'))
170 while (*n
>= '0' && *n
<= '9')
175 /* the tty prefix is optional */
176 if (strncmp(match
, "tty", 3) && strncmp(n
, "tty", 3) == 0)
178 if (strncmp(match
, n
, matchlen
))
180 cmatch
= (*(n
+ matchlen
) == 0);
181 debug1(" -> matched %s\n", match
);
183 sprintf(SockPath
+ sdirlen
, "/%s", name
);
185 debug1("stat %s\n", SockPath
);
187 debug2("uid = %d, gid = %d\n", getuid(), getgid());
188 debug2("euid = %d, egid = %d\n", geteuid(), getegid());
189 if (stat(SockPath
, &st
))
191 debug1("errno = %d\n", errno
);
195 #ifndef SOCK_NOT_IN_FS
198 debug("S_ISFIFO?\n");
199 if (!S_ISFIFO(st
.st_mode
))
204 debug("S_ISSOCK?\n");
205 if (!S_ISSOCK(st
.st_mode
))
211 debug2("st.st_uid = %d, real_uid = %d\n", st
.st_uid
, real_uid
);
212 if ((int)st
.st_uid
!= real_uid
)
214 mode
= (int)st
.st_mode
& 0777;
215 debug1(" has mode 0%03o\n", mode
);
217 if (multi
&& ((mode
& 0677) != 0601))
219 debug(" is not a MULTI-USER session");
220 if (strcmp(multi
, LoginName
))
222 debug(" and we are in a foreign directory.\n");
227 debug(", but it is our own session.\n");
231 debug(" store it.\n");
232 if ((sent
= (struct sent
*)malloc(sizeof(struct sent
))) == 0)
235 sent
->name
= SaveStr(name
);
238 slisttail
= &sent
->next
;
240 sockfd
= MakeClientSocket(0);
242 /* MakeClientSocket sets ids back to eff */
248 debug2(" MakeClientSocket failed, unreachable? %d %d\n",
251 #ifndef SOCKDIR_IS_LOCAL_TO_HOST
252 /* Unreachable - it is dead if we detect that it's local
253 * or we specified a match
255 n
= name
+ strlen(name
) - 1;
256 while (n
!= name
&& *n
!= '.')
258 if (matchlen
== 0 && !(*n
== '.' && n
[1] && strncmp(HostName
, n
+ 1, strlen(n
+ 1)) == 0))
260 npriv
++; /* a good socket that was not for us */
268 if (unlink(SockPath
) == 0)
278 /* Shall we connect ? */
279 debug2(" connecting: mode=%03o, rflag=%d, ", mode
, rflag
);
280 debug2("xflag=%d, dflag=%d ?\n", xflag
, dflag
);
283 * mode 600: socket is detached.
284 * mode 700: socket is attached.
285 * xflag implies rflag here.
287 * fail, when socket mode mode is not 600 or 700
288 * fail, when we want to detach w/o reattach, but it already is detached.
289 * fail, when we only want to attach, but mode 700 and not xflag.
290 * fail, if none of dflag, rflag, xflag is set.
292 if ((mode
!= 0700 && mode
!= 0600) ||
293 (dflag
&& !rflag
&& !xflag
&& mode
== 0600) ||
294 (!dflag
&& rflag
&& mode
== 0700 && !xflag
) ||
295 (!dflag
&& !rflag
&& !xflag
))
299 npriv
++; /* a good socket that was not for us */
305 if (fdp
&& (firsts
== -1 || (cmatch
&& nperfect
== 1)))
315 debug(" discarded.\n");
319 (void)closedir(dirp
);
320 if (!lsflag
&& nperfect
== 1)
322 if (nfound
&& (lsflag
|| ngood
!= 1) && !quietflag
)
327 Msg(0, nfound
> 1 ? "There are screens on:" : "There is a screen on:");
330 Msg(0, nfound
> 1 ? "There are several screens on:" : "There is a suitable screen on:");
333 Msg(0, "There are several suitable screens on:");
336 for (sent
= slist
; sent
; sent
= sent
->next
)
341 printf("\t%s\t(Attached)\n", sent
->name
);
344 printf("\t%s\t(Detached)\n", sent
->name
);
348 printf("\t%s\t(Multi, attached)\n", sent
->name
);
351 printf("\t%s\t(Multi, detached)\n", sent
->name
);
355 /* No trigraphs here! */
356 printf("\t%s\t(Dead ?%c?)\n", sent
->name
, '?');
359 printf("\t%s\t(Removed)\n", sent
->name
);
362 printf("\t%s\t(Remote or dead)\n", sent
->name
);
365 printf("\t%s\t(Private)\n", sent
->name
);
370 if (ndead
&& !quietflag
)
372 char *m
= "Remove dead screens with 'screen -wipe'.";
374 Msg(0, "%d socket%s wiped out.", nwipe
, nwipe
> 1 ? "s" : "");
376 Msg(0, m
, ndead
> 1 ? "s" : "", ndead
> 1 ? "" : "es"); /* other args for nethack */
380 sprintf(SockPath
+ sdirlen
, "/%s", firstn
);
384 SockPath
[sdirlen
] = 0;
385 for (sent
= slist
; sent
; sent
= nsent
)
398 *nfoundp
= nfound
- nwipe
;
405 ** Socket/pipe create routines
421 if ((s
= open(SockPath
, O_WRONLY
| O_NONBLOCK
)) >= 0)
423 debug("huii, my fifo already exists??\n");
426 Kill(D_userpid
, SIG_BYE
);
429 Msg(0, "There is already a screen running on %s.", Filename(SockPath
));
430 if (stat(SockPath
, &st
) == -1)
431 Panic(errno
, "stat");
432 if ((int)st
.st_uid
!= real_uid
)
433 Panic(0, "Unfortunatelly you are not its owner.");
434 if ((st
.st_mode
& 0700) == 0600)
435 Panic(0, "To resume it, use \"screen -r\"");
437 Panic(0, "It is not detached.");
441 (void) unlink(SockPath
);
442 if (mkfifo(SockPath
, SOCKMODE
))
443 Panic(0, "mkfifo %s failed", SockPath
);
445 if ((s
= open(SockPath
, O_RDWR
| O_NONBLOCK
, 0)) < 0)
447 if ((s
= open(SockPath
, O_RDONLY
| O_NONBLOCK
, 0)) < 0)
449 Panic(errno
, "open fifo %s", SockPath
);
453 # else /* !USE_SETEUID */
454 if (UserContext() > 0)
456 (void) unlink(SockPath
);
457 UserReturn(mkfifo(SockPath
, SOCKMODE
));
460 Panic(0, "mkfifo %s failed", SockPath
);
462 if ((s
= secopen(SockPath
, O_RDWR
| O_NONBLOCK
, 0)) < 0)
464 if ((s
= secopen(SockPath
, O_RDONLY
| O_NONBLOCK
, 0)) < 0)
466 Panic(errno
, "open fifo %s", SockPath
);
468 # endif /* !USE_SETEUID */
473 MakeClientSocket(err
)
478 if ((s
= secopen(SockPath
, O_WRONLY
| O_NONBLOCK
, 0)) >= 0)
480 (void) fcntl(s
, F_SETFL
, 0);
484 Msg(errno
, "%s", SockPath
);
485 debug2("MakeClientSocket() open %s failed (%d)\n", SockPath
, errno
);
490 #else /* NAMEDPIPE */
497 struct sockaddr_un a
;
500 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
501 Panic(errno
, "socket");
502 a
.sun_family
= AF_UNIX
;
503 strncpy(a
.sun_path
, SockPath
, sizeof(a
.sun_path
));
504 a
.sun_path
[sizeof(a
.sun_path
) - 1] = 0;
509 if (connect(s
, (struct sockaddr
*) &a
, strlen(SockPath
) + 2) != -1)
511 debug("oooooh! socket already is alive!\n");
514 Kill(D_userpid
, SIG_BYE
);
516 * oh, well. nobody receives that return code. papa
521 Msg(0, "There is already a screen running on %s.", Filename(SockPath
));
522 if (stat(SockPath
, &st
) == -1)
523 Panic(errno
, "stat");
524 if (st
.st_uid
!= real_uid
)
525 Panic(0, "Unfortunatelly you are not its owner.");
526 if ((st
.st_mode
& 0700) == 0600)
527 Panic(0, "To resume it, use \"screen -r\"");
529 Panic(0, "It is not detached.");
532 #if defined(m88k) || defined(sysV68)
533 close(s
); /* we get bind: Invalid argument if this is not done */
534 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
535 Panic(errno
, "reopen socket");
537 (void) unlink(SockPath
);
538 if (bind(s
, (struct sockaddr
*) & a
, strlen(SockPath
) + 2) == -1)
539 Panic(errno
, "bind (%s)", SockPath
);
540 #ifdef SOCK_NOT_IN_FS
543 if ((f
= secopen(SockPath
, O_RDWR
| O_CREAT
, SOCKMODE
)) < 0)
544 Panic(errno
, "shadow socket open");
548 chmod(SockPath
, SOCKMODE
);
550 chown(SockPath
, real_uid
, real_gid
);
552 #endif /* SOCK_NOT_IN_FS */
553 if (listen(s
, 5) == -1)
554 Panic(errno
, "listen");
556 fcntl(s
, F_SETOWN
, getpid());
557 debug1("Serversocket owned by %d\n", fcntl(s
, F_GETOWN
, 0));
558 # endif /* F_SETOWN */
567 MakeClientSocket(err
)
571 struct sockaddr_un a
;
573 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
574 Panic(errno
, "socket");
575 a
.sun_family
= AF_UNIX
;
576 strncpy(a
.sun_path
, SockPath
, sizeof(a
.sun_path
));
577 a
.sun_path
[sizeof(a
.sun_path
) - 1] = 0;
582 if (access(SockPath
, W_OK
))
585 Msg(errno
, "%s", SockPath
);
586 debug2("MakeClientSocket: access(%s): %d.\n", SockPath
, errno
);
591 if (connect(s
, (struct sockaddr
*) &a
, strlen(SockPath
) + 2) == -1)
594 Msg(errno
, "%s: connect", SockPath
);
595 debug("MakeClientSocket: connect failed.\n");
605 #endif /* NAMEDPIPE */
610 ** Message send and receive routines
615 SendCreateMsg(sty
, nwin
)
617 struct NewWindow
*nwin
;
623 char **av
= nwin
->args
;
626 if (strlen(sty
) > NAME_MAX
)
629 if (strlen(sty
) > 2 * MAXSTR
- 1)
630 sty
[2 * MAXSTR
- 1] = 0;
631 sprintf(SockPath
+ strlen(SockPath
), "/%s", sty
);
632 if ((s
= MakeClientSocket(1)) == -1)
634 debug1("SendCreateMsg() to '%s'\n", SockPath
);
635 bzero((char *)&m
, sizeof(m
));
637 strncpy(m
.m_tty
, attach_tty
, sizeof(m
.m_tty
) - 1);
638 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
641 if (nwin
->args
!= nwin_undef
.args
)
642 for (av
= nwin
->args
; *av
&& n
< MAXARGS
- 1; ++av
, ++n
)
644 len
= strlen(*av
) + 1;
645 if (p
+ len
>= m
.m
.create
.line
+ sizeof(m
.m
.create
.line
) - 1)
650 if (nwin
->aka
!= nwin_undef
.aka
&& p
+ strlen(nwin
->aka
) + 1 < m
.m
.create
.line
+ sizeof(m
.m
.create
.line
))
651 strcpy(p
, nwin
->aka
);
654 m
.m
.create
.nargs
= n
;
655 m
.m
.create
.aflag
= nwin
->aflag
;
656 m
.m
.create
.flowflag
= nwin
->flowflag
;
657 m
.m
.create
.lflag
= nwin
->lflag
;
658 m
.m
.create
.hheight
= nwin
->histheight
;
659 if (getcwd(m
.m
.create
.dir
, sizeof(m
.m
.create
.dir
)) == 0)
661 Msg(errno
, "getcwd");
664 if (nwin
->term
!= nwin_undef
.term
)
665 strncpy(m
.m
.create
.screenterm
, nwin
->term
, 19);
666 m
.m
.create
.screenterm
[19] = '\0';
667 m
.protocol_revision
= MSG_REVISION
;
668 debug1("SendCreateMsg writing '%s'\n", m
.m
.create
.line
);
669 if (write(s
, (char *) &m
, sizeof m
) != sizeof m
)
675 SendErrorMsg(tty
, buf
)
681 strncpy(m
.m
.message
, buf
, sizeof(m
.m
.message
) - 1);
682 m
.m
.message
[sizeof(m
.m
.message
) - 1] = 0;
683 s
= MakeClientSocket(0);
687 strncpy(m
.m_tty
, tty
, sizeof(m
.m_tty
) - 1);
688 m
.m_tty
[sizeof(m
.m_tty
) - 1] = 0;
689 m
.protocol_revision
= MSG_REVISION
;
690 debug1("SendErrorMsg(): writing to '%s'\n", SockPath
);
691 (void) write(s
, (char *) &m
, sizeof m
);
700 struct NewWindow nwin
;
703 register char **pp
= args
, *p
= mp
->m
.create
.line
;
706 n
= mp
->m
.create
.nargs
;
709 /* ugly hack alert... should be done by the frontend! */
716 if (IsNumColon(p
, 10, buf
, sizeof(buf
)))
721 if (num
< 0 || num
> MAXWIN
- 1)
738 nwin
.aflag
= mp
->m
.create
.aflag
;
739 nwin
.flowflag
= mp
->m
.create
.flowflag
;
740 if (*mp
->m
.create
.dir
)
741 nwin
.dir
= mp
->m
.create
.dir
;
742 nwin
.lflag
= mp
->m
.create
.lflag
;
743 nwin
.histheight
= mp
->m
.create
.hheight
;
744 if (*mp
->m
.create
.screenterm
)
745 nwin
.term
= mp
->m
.create
.screenterm
;
753 debug1("Checking pid %d\n", pid
);
756 if (eff_uid
== real_uid
)
758 if (UserContext() > 0)
759 UserReturn(kill(pid
, 0));
765 * From: "F. K. Bruner" <napalm@ugcs.caltech.edu>
766 * From: "Dan Egnor" <egnor@oracorp.com> Tue Aug 10 06:56:45 1993
767 * The problem is that under HPUX (and possibly other systems too) there are
768 * two equivalent device files for each pty/tty device:
769 * /dev/ttyxx == /dev/pty/ttyxx
770 * /dev/ptyxx == /dev/ptym/ptyxx
771 * I didn't look into the exact specifics, but I've run across this problem
772 * before: Even if you open /dev/ttyxx as fds 0 1 & 2 for a process, if that
773 * process calls the system to determine its tty, it'll get /dev/pty/ttyxx.
775 * Earlier versions seemed to work -- wonder what they did.
781 if (strlen(s1
) > 5) s1
+= strlen(s1
) - 5;
782 if (strlen(s2
) > 5) s2
+= strlen(s2
) - 5;
783 return strcmp(s1
, s2
);
785 # define TTYCMP(a, b) ttycmp(a, b)
787 # define TTYCMP(a, b) strcmp(a, b)
791 CreateTempDisplay(m
, recvfd
, wi
)
801 struct display
*olddisplays
= displays
;
806 pid
= m
->m
.attach
.apid
;
807 user
= m
->m
.attach
.auser
;
814 # endif /* POW_DETACH */
815 pid
= m
->m
.detach
.dpid
;
816 user
= m
->m
.detach
.duser
;
826 Msg(0, "Attach attempt with bad pid(%d)!", pid
);
834 myttyname
= ttyname(i
);
835 if (myttyname
== 0 || strcmp(myttyname
, m
->m_tty
))
837 Msg(errno
, "Attach: passed fd does not match tty: %s - %s!", m
->m_tty
, myttyname
? myttyname
: "NULL");
843 else if ((i
= secopen(m
->m_tty
, O_RDWR
| O_NONBLOCK
, 0)) < 0)
845 Msg(errno
, "Attach: Could not open %s!", m
->m_tty
);
854 #if defined(ultrix) || defined(pyr) || defined(NeXT)
855 brktty(i
); /* for some strange reason this must be done */
862 write(i
, "Attaching from inside of screen?\n", 33);
865 Msg(0, "Attach msg ignored: coming from inside.");
870 if (strcmp(user
, LoginName
))
871 if (*FindUserPtr(user
) == 0)
873 write(i
, "Access to session denied.\n", 26);
876 Msg(0, "Attach: access denied for user %s.", user
);
881 debug2("RecMsg: apid %d is o.k. and we just opened '%s'\n", pid
, m
->m_tty
);
885 write(i
, "Screen session in use.\n", 23);
893 /* create new display */
895 if (MakeDisplay(user
, m
->m_tty
, attach
? m
->m
.attach
.envterm
: "", i
, pid
, &Mode
) == 0)
897 write(i
, "Could not make display.\n", 24);
899 Msg(0, "Attach: could not make display for user %s", user
);
907 D_encoding
= m
->m
.attach
.encoding
== 1 ? UTF8
: m
->m
.attach
.encoding
? m
->m
.attach
.encoding
- 1 : 0;
909 D_encoding
= m
->m
.attach
.encoding
? m
->m
.attach
.encoding
- 1 : 0;
911 if (D_encoding
< 0 || !EncodingName(D_encoding
))
916 if (iflag
&& olddisplays
)
919 #if defined(TERMIO) || defined(POSIX)
920 olddisplays
->d_NewMode
.tio
.c_cc
[VINTR
] = VDISABLE
;
921 olddisplays
->d_NewMode
.tio
.c_lflag
&= ~ISIG
;
922 #else /* TERMIO || POSIX */
923 olddisplays
->d_NewMode
.m_tchars
.t_intrc
= -1;
924 #endif /* TERMIO || POSIX */
925 SetTTY(olddisplays
->d_userfd
, &olddisplays
->d_NewMode
);
927 SetMode(&D_OldMode
, &D_NewMode
, D_flow
, iflag
);
928 SetTTY(D_userfd
, &D_NewMode
);
929 if (fcntl(D_userfd
, F_SETFL
, FNBLOCK
))
930 Msg(errno
, "Warning: NBLOCK fcntl failed");
940 int ns
= ServerSocket
;
943 struct acluser
*user
;
946 debug("Ha, there was someone knocking on my fifo??\n");
947 if (fcntl(ServerSocket
, F_SETFL
, 0) == -1)
948 Panic(errno
, "BLOCK fcntl");
952 struct sockaddr_un a
;
958 debug("Ha, there was someone knocking on my socket??\n");
959 if ((ns
= accept(ns
, (struct sockaddr
*) &a
, (void *)&len
)) < 0)
961 Msg(errno
, "accept");
967 bzero(&msg
, sizeof(msg
));
972 msg
.msg_controllen
= sizeof(control
);
973 msg
.msg_control
= &control
;
976 len
= recvmsg(ns
, &msg
, 0);
977 if (len
< 0 && errno
== EINTR
)
985 if (msg
.msg_controllen
)
987 struct cmsghdr
*cmsg
;
988 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
))
992 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
)
994 cp
= (char *)CMSG_DATA(cmsg
);
996 while(cl
>= CMSG_LEN(sizeof(int)))
999 bcopy(cp
, &passedfd
, sizeof(int));
1000 if (recvfd
>= 0 && passedfd
!= recvfd
)
1003 cl
-= CMSG_LEN(sizeof(int));
1016 len
= read(ns
, p
, left
);
1017 if (len
< 0 && errno
== EINTR
)
1026 # ifndef BROKEN_PIPE
1027 /* Reopen pipe to prevent EOFs at the select() call */
1028 close(ServerSocket
);
1029 if ((ServerSocket
= secopen(SockPath
, O_RDONLY
| O_NONBLOCK
, 0)) < 0)
1030 Panic(errno
, "reopen fifo %s", SockPath
);
1032 serv_read
.fd
= ServerSocket
;
1048 if (left
!= sizeof(m
))
1049 Msg(0, "Message %d of %d bytes too small", left
, (int)sizeof(m
));
1051 debug("No data on socket.\n");
1054 if (m
.protocol_revision
!= MSG_REVISION
)
1058 Msg(0, "Invalid message (magic 0x%08x).", m
.protocol_revision
);
1062 debug2("*** RecMsg: type %d tty %s\n", m
.type
, m
.m_tty
);
1063 if (m
.type
!= MSG_ATTACH
&& recvfd
!= -1)
1069 for (display
= displays
; display
; display
= display
->d_next
)
1070 if (TTYCMP(D_usertty
, m
.m_tty
) == 0)
1072 debug2("display: %s display %sfound\n", m
.m_tty
, display
? "" : "not ");
1076 for (wi
= windows
; wi
; wi
= wi
->w_next
)
1077 if (!TTYCMP(m
.m_tty
, wi
->w_tty
))
1079 /* XXX: hmmm, rework this? */
1080 display
= wi
->w_layer
.l_cvlist
? wi
->w_layer
.l_cvlist
->c_display
: 0;
1081 debug2("but window %s %sfound.\n", m
.m_tty
, display
? "" :
1087 /* Remove the status to prevent garbage on the screen */
1088 if (display
&& D_status
)
1091 if (display
&& !D_tcinited
&& m
.type
!= MSG_HANGUP
)
1095 return; /* ignore messages for bad displays */
1102 CheckScreenSize(1); /* Change fore */
1106 * the window that issued the create message need not be an active
1107 * window. Then we create the window without having a display.
1108 * Resulting in another inactive window.
1113 if (display
&& D_userpid
!= 0 && kill(D_userpid
, 0) == 0)
1114 break; /* Intruder Alert */
1115 debug2("RecMsg: apid=%d,was %d\n", m
.m
.attach
.apid
, display
? D_userpid
: 0);
1119 if (CreateTempDisplay(&m
, recvfd
, wi
))
1122 if (D_user
->u_password
&& *D_user
->u_password
)
1129 Msg(0, "%s", m
.m
.message
);
1132 if (!wi
) /* ignore hangups from inside */
1135 #ifdef REMOTE_DETACH
1138 case MSG_POW_DETACH
:
1139 # endif /* POW_DETACH */
1141 user
= *FindUserPtr(m
.m
.detach
.duser
);
1142 if (user
&& user
->u_password
&& *user
->u_password
)
1144 if (CreateTempDisplay(&m
, recvfd
, 0))
1149 #endif /* PASSWORD */
1157 Msg(0, "Invalid message (type %d).", m
.type
);
1161 #if defined(_SEQUENT_) && !defined(NAMEDPIPE)
1164 * sequent_ptx socket emulation must have mode 000 on the socket!
1167 sconnect(s
, sapp
, len
)
1169 struct sockaddr
*sapp
;
1171 register struct sockaddr_un
*sap
;
1175 sap
= (struct sockaddr_un
*)sapp
;
1176 if (stat(sap
->sun_path
, &st
))
1178 chmod(sap
->sun_path
, 0);
1179 x
= connect(s
, (struct sockaddr
*) sap
, len
);
1180 chmod(sap
->sun_path
, st
.st_mode
);
1187 * Set the mode bits of the socket to the current status
1192 int r
, euid
= geteuid();
1193 if (euid
!= real_uid
)
1195 if (UserContext() <= 0)
1196 return UserStatus();
1198 r
= chmod(SockPath
, SOCKMODE
);
1200 * Sockets usually reside in the /tmp/ area, where sysadmin scripts
1201 * may be happy to remove old files. We manually prevent the socket
1202 * from becoming old. (chmod does not touch mtime).
1204 (void)utimes(SockPath
, NULL
);
1206 if (euid
!= real_uid
)
1212 * Try to recreate the socket/pipe
1217 close(ServerSocket
);
1218 if ((int)geteuid() != real_uid
)
1220 if (UserContext() > 0)
1221 UserReturn(unlink(SockPath
));
1225 (void) unlink(SockPath
);
1227 if ((ServerSocket
= MakeServerSocket()) < 0)
1230 serv_read
.fd
= ServerSocket
;
1248 if (m
->m
.attach
.detachfirst
!= MSG_ATTACH
)
1251 #if defined(pyr) || defined(xelos) || defined(sequent)
1253 * Kludge for systems with braindamaged termcap routines,
1254 * which evaluate $TERMCAP, regardless weather it describes
1255 * the correct terminal type or not.
1257 debug("unsetenv(TERMCAP) in case of a different terminal");
1258 unsetenv("TERMCAP");
1262 * We reboot our Terminal Emulator. Forget all we knew about
1263 * the old terminal, reread the termcap entries in .screenrc
1264 * (and nothing more from .screenrc is read. Mainly because
1265 * I did not check, weather a full reinit is safe. jw)
1266 * and /etc/screenrc, and initialise anew.
1272 extra_incap
= extra_outcap
= 0;
1273 debug2("Message says size (%dx%d)\n", m
->m
.attach
.columns
, m
->m
.attach
.lines
);
1275 # ifdef ALLOW_SYSSCREENRC
1276 if ((p
= getenv("SYSSCREENRC")))
1280 StartRc(ETCSCREENRC
, 1);
1282 StartRc(RcFileName
, 1);
1283 if (InitTermcap(m
->m
.attach
.columns
, m
->m
.attach
.lines
))
1289 MakeDefaultCanvas();
1290 InitTerm(m
->m
.attach
.adaptflag
); /* write init string on fd */
1291 if (displays
->d_next
== 0)
1293 signal(SIGHUP
, SigHup
);
1294 if (m
->m
.attach
.esc
!= -1 && m
->m
.attach
.meta_esc
!= -1)
1296 D_user
->u_Esc
= m
->m
.attach
.esc
;
1297 D_user
->u_MetaEsc
= m
->m
.attach
.meta_esc
;
1302 * we set the Utmp slots again, if we were detached normally
1303 * and if we were detached by ^Z.
1304 * don't log zomies back in!
1307 if (displays
->d_next
== 0)
1308 for (wi
= windows
; wi
; wi
= wi
->w_next
)
1309 if (wi
->w_ptyfd
>= 0 && wi
->w_slot
!= (slot_t
) -1)
1316 struct layout
*lay
= layout_attach
;
1317 if (lay
== &layout_last_marker
)
1321 LoadLayout(lay
, &D_canvas
);
1322 SetCanvasWindow(D_forecv
, 0);
1326 * there may be a window that we remember from last detach:
1328 debug1("D_user->u_detachwin = %d\n", D_user
->u_detachwin
);
1329 if (D_user
->u_detachwin
>= 0)
1330 fore
= wtab
[D_user
->u_detachwin
];
1334 /* Wayne wants us to restore the other window too. */
1335 if (D_user
->u_detachotherwin
>= 0)
1336 D_other
= wtab
[D_user
->u_detachotherwin
];
1339 if (*m
->m
.attach
.preselect
)
1341 if (!strcmp(m
->m
.attach
.preselect
, "="))
1343 else if (!strcmp(m
->m
.attach
.preselect
, "-"))
1348 else if (!strcmp(m
->m
.attach
.preselect
, "+"))
1350 struct action newscreen
;
1352 newscreen
.nr
= RC_SCREEN
;
1353 newscreen
.args
= &na
;
1354 DoAction(&newscreen
, -1);
1357 fore
= FindNiceWindow(fore
, m
->m
.attach
.preselect
);
1360 fore
= FindNiceWindow(fore
, 0);
1362 SetForeWindow(fore
);
1363 else if (!noshowwin
)
1366 if (!AclCheckPermCmd(D_user
, ACL_EXEC
, &comms
[RC_WINDOWLIST
]))
1369 flayer
= D_forecv
->c_layer
;
1370 display_wlist(1, WLIST_NUM
, (struct win
*)0);
1376 if (!D_fore
&& !noshowwin
)
1378 if (displays
->d_next
== 0 && console_window
)
1380 if (TtyGrabConsole(console_window
->w_ptyfd
, 1, "reattach") == 0)
1381 Msg(0, "console %s is on window %d", HostName
, console_window
->w_number
);
1383 debug("activated...\n");
1385 # if defined(DEBUG) && defined(SIG_NODEBUG)
1389 debug1("Attacher %d must not debug, as we have debug off.\n", pid
);
1390 kill(pid
, SIG_NODEBUG
);
1392 # endif /* SIG_NODEBUG */
1399 struct display
*next
, **d
, *det
;
1402 if (m
->type
== MSG_ATTACH
)
1405 pid
= m
->m
.detach
.dpid
;
1407 /* Remove the temporary display prompting for the password from the list */
1408 for (d
= &displays
; (det
= *d
); d
= &det
->d_next
)
1410 if (det
->d_userpid
== pid
)
1419 for (display
= displays
; display
; display
= next
)
1421 next
= display
->d_next
;
1423 if (m
->type
== MSG_POW_DETACH
)
1424 Detach(D_REMOTE_POWER
);
1426 # endif /* POW_DETACH */
1427 if (m
->type
== MSG_DETACH
)
1429 else if (m
->type
== MSG_ATTACH
)
1432 if (m
->m
.attach
.detachfirst
== MSG_POW_DETACH
)
1433 Detach(D_REMOTE_POWER
);
1436 if (m
->m
.attach
.detachfirst
== MSG_DETACH
)
1440 display
= displays
= det
;
1441 if (m
->type
!= MSG_ATTACH
)
1450 static void PasswordProcessInput
__P((char *, int));
1462 struct pwdata
*pwdata
;
1464 pwdata
= (struct pwdata
*)malloc(sizeof(struct pwdata
));
1469 D_processinputdata
= (char *)pwdata
;
1470 D_processinput
= PasswordProcessInput
;
1471 AddStr("Screen password: ");
1475 PasswordProcessInput(ibuf
, ilen
)
1479 struct pwdata
*pwdata
;
1482 int pid
= D_userpid
;
1484 pwdata
= (struct pwdata
*)D_processinputdata
;
1488 c
= *(unsigned char *)ibuf
++;
1489 if (c
== '\r' || c
== '\n')
1491 up
= D_user
->u_password
;
1493 if (strncmp(crypt(pwdata
->buf
, up
), up
, strlen(up
)))
1495 /* uh oh, user failed */
1496 bzero(pwdata
->buf
, sizeof(pwdata
->buf
));
1497 AddStr("\r\nPassword incorrect.\r\n");
1498 D_processinputdata
= 0; /* otherwise freed by FreeDis */
1500 Msg(0, "Illegal reattach attempt from terminal %s.", pwdata
->m
.m_tty
);
1505 /* great, pw matched, all is fine */
1506 bzero(pwdata
->buf
, sizeof(pwdata
->buf
));
1508 D_processinputdata
= 0;
1509 D_processinput
= ProcessInput
;
1510 if (pwdata
->m
.type
== MSG_ATTACH
)
1511 FinishAttach(&pwdata
->m
);
1513 FinishDetach(&pwdata
->m
);
1524 if (c
== '\b' || c
== 0177)
1535 if (l
< (int)sizeof(pwdata
->buf
) - 1)
1536 pwdata
->buf
[l
++] = c
;
1546 char *args
[MAXARGS
];
1549 register char **pp
= args
, *p
= mp
->m
.command
.cmd
;
1550 struct acluser
*user
;
1552 extern struct acluser
*EffectiveAclUser
; /* acls.c */
1554 extern struct acluser
*users
; /* acls.c */
1558 n
= mp
->m
.command
.nargs
;
1559 if (n
> MAXARGS
- 1)
1569 user
= *FindUserPtr(mp
->m
.attach
.auser
);
1572 Msg(0, "Unknown user %s tried to send a command!", mp
->m
.attach
.auser
);
1579 if (user
->u_password
&& *user
->u_password
)
1581 Msg(0, "User %s has a password, cannot use -X option.", mp
->m
.attach
.auser
);
1586 for (display
= displays
; display
; display
= display
->d_next
)
1589 for (fore
= windows
; fore
; fore
= fore
->w_next
)
1590 if (!TTYCMP(mp
->m_tty
, fore
->w_tty
))
1593 display
= fore
->w_layer
.l_cvlist
? fore
->w_layer
.l_cvlist
->c_display
: 0;
1597 display
= displays
; /* sigh */
1598 if (*mp
->m
.command
.preselect
)
1601 if (strcmp(mp
->m
.command
.preselect
, "-"))
1602 i
= WindowByNoN(mp
->m
.command
.preselect
);
1603 fore
= i
>= 0 ? wtab
[i
] : 0;
1607 if (display
&& D_user
== user
)
1608 fore
= Layer2Window(display
->d_forecv
->c_layer
);
1611 fore
= user
->u_detachwin
>= 0 ? wtab
[user
->u_detachwin
] : 0;
1612 fore
= FindNiceWindow(fore
, 0);
1616 EffectiveAclUser
= user
;
1620 char *oldrcname
= rc_name
;
1622 debug3("Running command on display %x window %x (%d)\n", display
, fore
, fore
? fore
->w_number
: -1);
1623 flayer
= fore
? &fore
->w_layer
: 0;
1624 if (fore
&& fore
->w_savelayer
&& (fore
->w_blocked
|| fore
->w_savelayer
->l_cvlist
== 0))
1625 flayer
= fore
->w_savelayer
;
1626 DoCommand(args
, argl
);
1627 rc_name
= oldrcname
;
1630 EffectiveAclUser
= 0;
1637 SendAttachMsg(s
, m
, fd
)
1645 char buf
[CMSG_SPACE(sizeof(int))];
1646 struct cmsghdr
*cmsg
;
1648 iov
.iov_base
= (char *)m
;
1649 iov
.iov_len
= sizeof(*m
);
1650 bzero(&msg
, sizeof(msg
));
1652 msg
.msg_namelen
= 0;
1655 msg
.msg_control
= buf
;
1656 msg
.msg_controllen
= sizeof(buf
);
1657 cmsg
= CMSG_FIRSTHDR(&msg
);
1658 cmsg
->cmsg_level
= SOL_SOCKET
;
1659 cmsg
->cmsg_type
= SCM_RIGHTS
;
1660 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
1661 bcopy(&fd
, CMSG_DATA(cmsg
), sizeof(int));
1662 msg
.msg_controllen
= cmsg
->cmsg_len
;
1665 r
= sendmsg(s
, &msg
, 0);
1666 if (r
== -1 && errno
== EINTR
)