announce.c revision 1.6 1 /*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)announce.c 5.9 (Berkeley) 2/26/91";*/
36 static char rcsid[] = "$Id: announce.c,v 1.6 1996/07/17 18:46:58 rat Exp $";
37 #endif /* not lint */
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <sys/wait.h>
43 #include <sys/socket.h>
44 #include <protocols/talkd.h>
45 #include <sgtty.h>
46 #include <errno.h>
47 #include <syslog.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <vis.h>
53 #include <paths.h>
54
55 extern char hostname[];
56
57 /*
58 * Announce an invitation to talk.
59 *
60 * Because the tty driver insists on attaching a terminal-less
61 * process to any terminal that it writes on, we must fork a child
62 * to protect ourselves
63 */
64 announce(request, remote_machine)
65 CTL_MSG *request;
66 char *remote_machine;
67 {
68 int pid, val, status;
69
70 if (pid = fork()) {
71 /* we are the parent, so wait for the child */
72 if (pid == -1) /* the fork failed */
73 return (FAILED);
74 do {
75 val = wait(&status);
76 if (val == -1) {
77 if (errno == EINTR)
78 continue;
79 /* shouldn't happen */
80 syslog(LOG_WARNING, "announce: wait: %m");
81 return (FAILED);
82 }
83 } while (val != pid);
84 if (status&0377 > 0) /* we were killed by some signal */
85 return (FAILED);
86 /* Get the second byte, this is the exit/return code */
87 return ((status >> 8) & 0377);
88 }
89 /* we are the child, go and do it */
90 _exit(announce_proc(request, remote_machine));
91 }
92
93 /*
94 * See if the user is accepting messages. If so, announce that
95 * a talk is requested.
96 */
97 announce_proc(request, remote_machine)
98 CTL_MSG *request;
99 char *remote_machine;
100 {
101 int pid, status;
102 char full_tty[32];
103 FILE *tf;
104 struct stat stbuf;
105
106 (void)snprintf(full_tty, sizeof(full_tty), "%s/%s", _PATH_DEV,
107 request->r_tty);
108 if (access(full_tty, 0) != 0)
109 return (FAILED);
110 if ((tf = fopen(full_tty, "w")) == NULL)
111 return (PERMISSION_DENIED);
112 /*
113 * On first tty open, the server will have
114 * it's pgrp set, so disconnect us from the
115 * tty before we catch a signal.
116 */
117 ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
118 if (fstat(fileno(tf), &stbuf) < 0)
119 return (PERMISSION_DENIED);
120 if ((stbuf.st_mode&020) == 0)
121 return (PERMISSION_DENIED);
122 print_mesg(tf, request, remote_machine);
123 fclose(tf);
124 return (SUCCESS);
125 }
126
127 #define max(a,b) ( (a) > (b) ? (a) : (b) )
128 #define N_LINES 5
129 #define N_CHARS 120
130
131 /*
132 * Build a block of characters containing the message.
133 * It is sent blank filled and in a single block to
134 * try to keep the message in one piece if the recipient
135 * in in vi at the time
136 */
137 print_mesg(tf, request, remote_machine)
138 FILE *tf;
139 CTL_MSG *request;
140 char *remote_machine;
141 {
142 struct timeval clock;
143 time_t clocktime;
144 struct timezone zone;
145 struct tm *localclock;
146 char line_buf[N_LINES][N_CHARS];
147 int sizes[N_LINES];
148 char big_buf[N_LINES*N_CHARS];
149 char *bptr, *lptr, *vis_user;
150 int i, j, max_size;
151
152 i = 0;
153 max_size = 0;
154 gettimeofday(&clock, &zone);
155 clocktime = clock.tv_sec;
156 localclock = localtime(&clocktime);
157 (void)snprintf(line_buf[i], N_CHARS, " ");
158 sizes[i] = strlen(line_buf[i]);
159 max_size = max(max_size, sizes[i]);
160 i++;
161 (void)snprintf(line_buf[i], N_CHARS,
162 "Message from Talk_Daemon@%s at %d:%02d ...",
163 hostname, localclock->tm_hour , localclock->tm_min );
164 sizes[i] = strlen(line_buf[i]);
165 max_size = max(max_size, sizes[i]);
166 i++;
167 vis_user = (char *) malloc(strlen(request->l_name) * 4 + 1);
168 strvis(vis_user, request->l_name, VIS_CSTYLE);
169 (void)snprintf(line_buf[i], N_CHARS,
170 "talk: connection requested by %s@%s.", vis_user, remote_machine);
171 sizes[i] = strlen(line_buf[i]);
172 max_size = max(max_size, sizes[i]);
173 i++;
174 (void)snprintf(line_buf[i], N_CHARS,
175 "talk: respond with: talk %s@%s", vis_user, remote_machine);
176 sizes[i] = strlen(line_buf[i]);
177 max_size = max(max_size, sizes[i]);
178 i++;
179 (void)snprintf(line_buf[i], N_CHARS, " ");
180 sizes[i] = strlen(line_buf[i]);
181 max_size = max(max_size, sizes[i]);
182 i++;
183 bptr = big_buf;
184 *bptr++ = ''; /* send something to wake them up */
185 *bptr++ = '\r'; /* add a \r in case of raw mode */
186 *bptr++ = '\n';
187 for (i = 0; i < N_LINES; i++) {
188 /* copy the line into the big buffer */
189 lptr = line_buf[i];
190 while (*lptr != '\0')
191 *(bptr++) = *(lptr++);
192 /* pad out the rest of the lines with blanks */
193 for (j = sizes[i]; j < max_size + 2; j++)
194 *(bptr++) = ' ';
195 *(bptr++) = '\r'; /* add a \r in case of raw mode */
196 *(bptr++) = '\n';
197 }
198 *bptr = '\0';
199 fprintf(tf, big_buf);
200 fflush(tf);
201 ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
202 }
203