announce.c revision 1.21.28.1 1 1.21.28.1 keiichi /* $NetBSD: announce.c,v 1.21.28.1 2008/03/24 07:14:46 keiichi Exp $ */
2 1.7 christos
3 1.1 cgd /*
4 1.7 christos * Copyright (c) 1983, 1993
5 1.7 christos * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.21 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.7 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.7 christos #if 0
35 1.7 christos static char sccsid[] = "@(#)announce.c 8.3 (Berkeley) 4/28/95";
36 1.7 christos #else
37 1.21.28.1 keiichi __RCSID("$NetBSD: announce.c,v 1.21.28.1 2008/03/24 07:14:46 keiichi Exp $");
38 1.7 christos #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #include <sys/types.h>
42 1.7 christos #include <sys/uio.h>
43 1.1 cgd #include <sys/stat.h>
44 1.1 cgd #include <sys/time.h>
45 1.1 cgd #include <sys/wait.h>
46 1.1 cgd #include <sys/socket.h>
47 1.1 cgd #include <protocols/talkd.h>
48 1.1 cgd #include <errno.h>
49 1.1 cgd #include <syslog.h>
50 1.1 cgd #include <unistd.h>
51 1.1 cgd #include <stdio.h>
52 1.4 cgd #include <stdlib.h>
53 1.1 cgd #include <string.h>
54 1.9 kleink #include <time.h>
55 1.3 glass #include <vis.h>
56 1.1 cgd #include <paths.h>
57 1.8 christos #include <util.h>
58 1.8 christos #include "extern.h"
59 1.1 cgd
60 1.1 cgd extern char hostname[];
61 1.1 cgd
62 1.1 cgd /*
63 1.1 cgd * Announce an invitation to talk.
64 1.1 cgd */
65 1.1 cgd
66 1.1 cgd /*
67 1.15 enami * See if the user is accepting messages. If so, announce that
68 1.1 cgd * a talk is requested.
69 1.1 cgd */
70 1.8 christos int
71 1.7 christos announce(request, remote_machine)
72 1.1 cgd CTL_MSG *request;
73 1.1 cgd char *remote_machine;
74 1.1 cgd {
75 1.1 cgd char full_tty[32];
76 1.1 cgd struct stat stbuf;
77 1.1 cgd
78 1.7 christos (void)snprintf(full_tty, sizeof(full_tty),
79 1.7 christos "%s%s", _PATH_DEV, request->r_tty);
80 1.10 mrg if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&S_IWGRP) == 0)
81 1.1 cgd return (PERMISSION_DENIED);
82 1.8 christos return (print_mesg(request->r_tty, request, remote_machine));
83 1.1 cgd }
84 1.1 cgd
85 1.15 enami #define max(a, b) ((a) > (b) ? (a) : (b))
86 1.1 cgd #define N_LINES 5
87 1.7 christos #define N_CHARS 256
88 1.1 cgd
89 1.1 cgd /*
90 1.15 enami * Build a block of characters containing the message.
91 1.1 cgd * It is sent blank filled and in a single block to
92 1.1 cgd * try to keep the message in one piece if the recipient
93 1.21.28.1 keiichi * is in vi at the time.
94 1.1 cgd */
95 1.8 christos int
96 1.8 christos print_mesg(tty, request, remote_machine)
97 1.7 christos char *tty;
98 1.1 cgd CTL_MSG *request;
99 1.1 cgd char *remote_machine;
100 1.1 cgd {
101 1.1 cgd struct timeval clock;
102 1.5 cgd time_t clocktime;
103 1.1 cgd struct tm *localclock;
104 1.7 christos struct iovec iovec;
105 1.1 cgd char line_buf[N_LINES][N_CHARS];
106 1.1 cgd int sizes[N_LINES];
107 1.13 tv char big_buf[(N_LINES + 1) * N_CHARS];
108 1.20 itojun char *bptr, *lptr, vis_user[sizeof(request->l_name) * 4];
109 1.1 cgd int i, j, max_size;
110 1.1 cgd
111 1.1 cgd i = 0;
112 1.1 cgd max_size = 0;
113 1.21.28.1 keiichi (void)gettimeofday(&clock, NULL);
114 1.5 cgd clocktime = clock.tv_sec;
115 1.5 cgd localclock = localtime(&clocktime);
116 1.6 rat (void)snprintf(line_buf[i], N_CHARS, " ");
117 1.1 cgd sizes[i] = strlen(line_buf[i]);
118 1.1 cgd max_size = max(max_size, sizes[i]);
119 1.1 cgd i++;
120 1.10 mrg
121 1.15 enami (void)snprintf(line_buf[i], N_CHARS,
122 1.6 rat "Message from Talk_Daemon@%s at %d:%02d ...",
123 1.15 enami hostname, localclock->tm_hour, localclock->tm_min);
124 1.1 cgd sizes[i] = strlen(line_buf[i]);
125 1.1 cgd max_size = max(max_size, sizes[i]);
126 1.1 cgd i++;
127 1.20 itojun if (strlen(request->l_name) + 1 > sizeof(vis_user) / 4)
128 1.17 itojun return (FAILED);
129 1.3 glass strvis(vis_user, request->l_name, VIS_CSTYLE);
130 1.15 enami (void)snprintf(line_buf[i], N_CHARS,
131 1.6 rat "talk: connection requested by %s@%s.", vis_user, remote_machine);
132 1.1 cgd sizes[i] = strlen(line_buf[i]);
133 1.1 cgd max_size = max(max_size, sizes[i]);
134 1.1 cgd i++;
135 1.6 rat (void)snprintf(line_buf[i], N_CHARS,
136 1.6 rat "talk: respond with: talk %s@%s", vis_user, remote_machine);
137 1.1 cgd sizes[i] = strlen(line_buf[i]);
138 1.1 cgd max_size = max(max_size, sizes[i]);
139 1.1 cgd i++;
140 1.6 rat (void)snprintf(line_buf[i], N_CHARS, " ");
141 1.1 cgd sizes[i] = strlen(line_buf[i]);
142 1.1 cgd max_size = max(max_size, sizes[i]);
143 1.1 cgd i++;
144 1.1 cgd bptr = big_buf;
145 1.15 enami *bptr++ = '\a'; /* send something to wake them up */
146 1.15 enami *bptr++ = '\r'; /* add a \r in case of raw mode */
147 1.1 cgd *bptr++ = '\n';
148 1.1 cgd for (i = 0; i < N_LINES; i++) {
149 1.1 cgd /* copy the line into the big buffer */
150 1.1 cgd lptr = line_buf[i];
151 1.11 mrg while (*lptr != '\0' && lptr < (line_buf[i] + N_CHARS))
152 1.1 cgd *(bptr++) = *(lptr++);
153 1.1 cgd /* pad out the rest of the lines with blanks */
154 1.1 cgd for (j = sizes[i]; j < max_size + 2; j++)
155 1.1 cgd *(bptr++) = ' ';
156 1.1 cgd *(bptr++) = '\r'; /* add a \r in case of raw mode */
157 1.1 cgd *(bptr++) = '\n';
158 1.1 cgd }
159 1.1 cgd *bptr = '\0';
160 1.7 christos iovec.iov_base = big_buf;
161 1.7 christos iovec.iov_len = bptr - big_buf;
162 1.7 christos /*
163 1.7 christos * we choose a timeout of RING_WAIT-5 seconds so that we don't
164 1.7 christos * stack up processes trying to write messages to a tty
165 1.7 christos * that is permanently blocked.
166 1.7 christos */
167 1.7 christos if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)
168 1.7 christos return (FAILED);
169 1.7 christos
170 1.7 christos return (SUCCESS);
171 1.1 cgd }
172