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