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