announce.c revision 1.1 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.1 cgd static char sccsid[] = "@(#)announce.c 5.9 (Berkeley) 2/26/91";
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #include <sys/types.h>
39 1.1 cgd #include <sys/stat.h>
40 1.1 cgd #include <sys/time.h>
41 1.1 cgd #include <sys/wait.h>
42 1.1 cgd #include <sys/socket.h>
43 1.1 cgd #include <protocols/talkd.h>
44 1.1 cgd #include <sgtty.h>
45 1.1 cgd #include <errno.h>
46 1.1 cgd #include <syslog.h>
47 1.1 cgd #include <unistd.h>
48 1.1 cgd #include <stdio.h>
49 1.1 cgd #include <string.h>
50 1.1 cgd #include <paths.h>
51 1.1 cgd
52 1.1 cgd extern char hostname[];
53 1.1 cgd
54 1.1 cgd /*
55 1.1 cgd * Announce an invitation to talk.
56 1.1 cgd *
57 1.1 cgd * Because the tty driver insists on attaching a terminal-less
58 1.1 cgd * process to any terminal that it writes on, we must fork a child
59 1.1 cgd * to protect ourselves
60 1.1 cgd */
61 1.1 cgd announce(request, remote_machine)
62 1.1 cgd CTL_MSG *request;
63 1.1 cgd char *remote_machine;
64 1.1 cgd {
65 1.1 cgd int pid, val, status;
66 1.1 cgd
67 1.1 cgd if (pid = fork()) {
68 1.1 cgd /* we are the parent, so wait for the child */
69 1.1 cgd if (pid == -1) /* the fork failed */
70 1.1 cgd return (FAILED);
71 1.1 cgd do {
72 1.1 cgd val = wait(&status);
73 1.1 cgd if (val == -1) {
74 1.1 cgd if (errno == EINTR)
75 1.1 cgd continue;
76 1.1 cgd /* shouldn't happen */
77 1.1 cgd syslog(LOG_WARNING, "announce: wait: %m");
78 1.1 cgd return (FAILED);
79 1.1 cgd }
80 1.1 cgd } while (val != pid);
81 1.1 cgd if (status&0377 > 0) /* we were killed by some signal */
82 1.1 cgd return (FAILED);
83 1.1 cgd /* Get the second byte, this is the exit/return code */
84 1.1 cgd return ((status >> 8) & 0377);
85 1.1 cgd }
86 1.1 cgd /* we are the child, go and do it */
87 1.1 cgd _exit(announce_proc(request, remote_machine));
88 1.1 cgd }
89 1.1 cgd
90 1.1 cgd /*
91 1.1 cgd * See if the user is accepting messages. If so, announce that
92 1.1 cgd * a talk is requested.
93 1.1 cgd */
94 1.1 cgd announce_proc(request, remote_machine)
95 1.1 cgd CTL_MSG *request;
96 1.1 cgd char *remote_machine;
97 1.1 cgd {
98 1.1 cgd int pid, status;
99 1.1 cgd char full_tty[32];
100 1.1 cgd FILE *tf;
101 1.1 cgd struct stat stbuf;
102 1.1 cgd
103 1.1 cgd (void)sprintf(full_tty, "%s/%s", _PATH_DEV, request->r_tty);
104 1.1 cgd if (access(full_tty, 0) != 0)
105 1.1 cgd return (FAILED);
106 1.1 cgd if ((tf = fopen(full_tty, "w")) == NULL)
107 1.1 cgd return (PERMISSION_DENIED);
108 1.1 cgd /*
109 1.1 cgd * On first tty open, the server will have
110 1.1 cgd * it's pgrp set, so disconnect us from the
111 1.1 cgd * tty before we catch a signal.
112 1.1 cgd */
113 1.1 cgd ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
114 1.1 cgd if (fstat(fileno(tf), &stbuf) < 0)
115 1.1 cgd return (PERMISSION_DENIED);
116 1.1 cgd if ((stbuf.st_mode&020) == 0)
117 1.1 cgd return (PERMISSION_DENIED);
118 1.1 cgd print_mesg(tf, request, remote_machine);
119 1.1 cgd fclose(tf);
120 1.1 cgd return (SUCCESS);
121 1.1 cgd }
122 1.1 cgd
123 1.1 cgd #define max(a,b) ( (a) > (b) ? (a) : (b) )
124 1.1 cgd #define N_LINES 5
125 1.1 cgd #define N_CHARS 120
126 1.1 cgd
127 1.1 cgd /*
128 1.1 cgd * Build a block of characters containing the message.
129 1.1 cgd * It is sent blank filled and in a single block to
130 1.1 cgd * try to keep the message in one piece if the recipient
131 1.1 cgd * in in vi at the time
132 1.1 cgd */
133 1.1 cgd print_mesg(tf, request, remote_machine)
134 1.1 cgd FILE *tf;
135 1.1 cgd CTL_MSG *request;
136 1.1 cgd char *remote_machine;
137 1.1 cgd {
138 1.1 cgd struct timeval clock;
139 1.1 cgd struct timezone zone;
140 1.1 cgd struct tm *localtime();
141 1.1 cgd struct tm *localclock;
142 1.1 cgd char line_buf[N_LINES][N_CHARS];
143 1.1 cgd int sizes[N_LINES];
144 1.1 cgd char big_buf[N_LINES*N_CHARS];
145 1.1 cgd char *bptr, *lptr;
146 1.1 cgd int i, j, max_size;
147 1.1 cgd
148 1.1 cgd i = 0;
149 1.1 cgd max_size = 0;
150 1.1 cgd gettimeofday(&clock, &zone);
151 1.1 cgd localclock = localtime( &clock.tv_sec );
152 1.1 cgd (void)sprintf(line_buf[i], " ");
153 1.1 cgd sizes[i] = strlen(line_buf[i]);
154 1.1 cgd max_size = max(max_size, sizes[i]);
155 1.1 cgd i++;
156 1.1 cgd (void)sprintf(line_buf[i], "Message from Talk_Daemon@%s at %d:%02d ...",
157 1.1 cgd hostname, localclock->tm_hour , localclock->tm_min );
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.1 cgd (void)sprintf(line_buf[i], "talk: connection requested by %s@%s.",
162 1.1 cgd request->l_name, remote_machine);
163 1.1 cgd sizes[i] = strlen(line_buf[i]);
164 1.1 cgd max_size = max(max_size, sizes[i]);
165 1.1 cgd i++;
166 1.1 cgd (void)sprintf(line_buf[i], "talk: respond with: talk %s@%s",
167 1.1 cgd request->l_name, 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], " ");
172 1.1 cgd sizes[i] = strlen(line_buf[i]);
173 1.1 cgd max_size = max(max_size, sizes[i]);
174 1.1 cgd i++;
175 1.1 cgd bptr = big_buf;
176 1.1 cgd *bptr++ = ''; /* send something to wake them up */
177 1.1 cgd *bptr++ = '\r'; /* add a \r in case of raw mode */
178 1.1 cgd *bptr++ = '\n';
179 1.1 cgd for (i = 0; i < N_LINES; i++) {
180 1.1 cgd /* copy the line into the big buffer */
181 1.1 cgd lptr = line_buf[i];
182 1.1 cgd while (*lptr != '\0')
183 1.1 cgd *(bptr++) = *(lptr++);
184 1.1 cgd /* pad out the rest of the lines with blanks */
185 1.1 cgd for (j = sizes[i]; j < max_size + 2; j++)
186 1.1 cgd *(bptr++) = ' ';
187 1.1 cgd *(bptr++) = '\r'; /* add a \r in case of raw mode */
188 1.1 cgd *(bptr++) = '\n';
189 1.1 cgd }
190 1.1 cgd *bptr = '\0';
191 1.1 cgd fprintf(tf, big_buf);
192 1.1 cgd fflush(tf);
193 1.1 cgd ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
194 1.1 cgd }
195