announce.c revision 1.4 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.4 cgd static char rcsid[] = "$Id: announce.c,v 1.4 1994/12/23 15:59:21 cgd 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.1 cgd (void)sprintf(full_tty, "%s/%s", _PATH_DEV, request->r_tty);
107 1.1 cgd if (access(full_tty, 0) != 0)
108 1.1 cgd return (FAILED);
109 1.1 cgd if ((tf = fopen(full_tty, "w")) == NULL)
110 1.1 cgd return (PERMISSION_DENIED);
111 1.1 cgd /*
112 1.1 cgd * On first tty open, the server will have
113 1.1 cgd * it's pgrp set, so disconnect us from the
114 1.1 cgd * tty before we catch a signal.
115 1.1 cgd */
116 1.1 cgd ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
117 1.1 cgd if (fstat(fileno(tf), &stbuf) < 0)
118 1.1 cgd return (PERMISSION_DENIED);
119 1.1 cgd if ((stbuf.st_mode&020) == 0)
120 1.1 cgd return (PERMISSION_DENIED);
121 1.1 cgd print_mesg(tf, request, remote_machine);
122 1.1 cgd fclose(tf);
123 1.1 cgd return (SUCCESS);
124 1.1 cgd }
125 1.1 cgd
126 1.1 cgd #define max(a,b) ( (a) > (b) ? (a) : (b) )
127 1.1 cgd #define N_LINES 5
128 1.1 cgd #define N_CHARS 120
129 1.1 cgd
130 1.1 cgd /*
131 1.1 cgd * Build a block of characters containing the message.
132 1.1 cgd * It is sent blank filled and in a single block to
133 1.1 cgd * try to keep the message in one piece if the recipient
134 1.1 cgd * in in vi at the time
135 1.1 cgd */
136 1.1 cgd print_mesg(tf, request, remote_machine)
137 1.1 cgd FILE *tf;
138 1.1 cgd CTL_MSG *request;
139 1.1 cgd char *remote_machine;
140 1.1 cgd {
141 1.1 cgd struct timeval clock;
142 1.1 cgd struct timezone zone;
143 1.1 cgd struct tm *localtime();
144 1.1 cgd struct tm *localclock;
145 1.1 cgd char line_buf[N_LINES][N_CHARS];
146 1.1 cgd int sizes[N_LINES];
147 1.1 cgd char big_buf[N_LINES*N_CHARS];
148 1.3 glass char *bptr, *lptr, *vis_user;
149 1.1 cgd int i, j, max_size;
150 1.1 cgd
151 1.1 cgd i = 0;
152 1.1 cgd max_size = 0;
153 1.1 cgd gettimeofday(&clock, &zone);
154 1.1 cgd localclock = localtime( &clock.tv_sec );
155 1.1 cgd (void)sprintf(line_buf[i], " ");
156 1.1 cgd sizes[i] = strlen(line_buf[i]);
157 1.1 cgd max_size = max(max_size, sizes[i]);
158 1.1 cgd i++;
159 1.1 cgd (void)sprintf(line_buf[i], "Message from Talk_Daemon@%s at %d:%02d ...",
160 1.1 cgd hostname, localclock->tm_hour , localclock->tm_min );
161 1.1 cgd sizes[i] = strlen(line_buf[i]);
162 1.1 cgd max_size = max(max_size, sizes[i]);
163 1.1 cgd i++;
164 1.3 glass vis_user = (char *) malloc(strlen(request->l_name) * 4 + 1);
165 1.3 glass strvis(vis_user, request->l_name, VIS_CSTYLE);
166 1.1 cgd (void)sprintf(line_buf[i], "talk: connection requested by %s@%s.",
167 1.3 glass vis_user, remote_machine);
168 1.1 cgd sizes[i] = strlen(line_buf[i]);
169 1.1 cgd max_size = max(max_size, sizes[i]);
170 1.1 cgd i++;
171 1.1 cgd (void)sprintf(line_buf[i], "talk: respond with: talk %s@%s",
172 1.3 glass vis_user, remote_machine);
173 1.1 cgd sizes[i] = strlen(line_buf[i]);
174 1.1 cgd max_size = max(max_size, sizes[i]);
175 1.1 cgd i++;
176 1.1 cgd (void)sprintf(line_buf[i], " ");
177 1.1 cgd sizes[i] = strlen(line_buf[i]);
178 1.1 cgd max_size = max(max_size, sizes[i]);
179 1.1 cgd i++;
180 1.1 cgd bptr = big_buf;
181 1.1 cgd *bptr++ = ''; /* send something to wake them up */
182 1.1 cgd *bptr++ = '\r'; /* add a \r in case of raw mode */
183 1.1 cgd *bptr++ = '\n';
184 1.1 cgd for (i = 0; i < N_LINES; i++) {
185 1.1 cgd /* copy the line into the big buffer */
186 1.1 cgd lptr = line_buf[i];
187 1.1 cgd while (*lptr != '\0')
188 1.1 cgd *(bptr++) = *(lptr++);
189 1.1 cgd /* pad out the rest of the lines with blanks */
190 1.1 cgd for (j = sizes[i]; j < max_size + 2; j++)
191 1.1 cgd *(bptr++) = ' ';
192 1.1 cgd *(bptr++) = '\r'; /* add a \r in case of raw mode */
193 1.1 cgd *(bptr++) = '\n';
194 1.1 cgd }
195 1.1 cgd *bptr = '\0';
196 1.1 cgd fprintf(tf, big_buf);
197 1.1 cgd fflush(tf);
198 1.1 cgd ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
199 1.1 cgd }
200