announce.c revision 177626
1292932Sdim/*
2292932Sdim * Copyright (c) 1983, 1993
3353358Sdim *	The Regents of the University of California.  All rights reserved.
4353358Sdim *
5353358Sdim * Redistribution and use in source and binary forms, with or without
6292932Sdim * modification, are permitted provided that the following conditions
7292932Sdim * are met:
8292932Sdim * 1. Redistributions of source code must retain the above copyright
9292932Sdim *    notice, this list of conditions and the following disclaimer.
10292932Sdim * 2. Redistributions in binary form must reproduce the above copyright
11292932Sdim *    notice, this list of conditions and the following disclaimer in the
12292932Sdim *    documentation and/or other materials provided with the distribution.
13321369Sdim * 3. All advertising materials mentioning features or use of this software
14321369Sdim *    must display the following acknowledgement:
15292932Sdim *	This product includes software developed by the University of
16314564Sdim *	California, Berkeley and its contributors.
17314564Sdim * 4. Neither the name of the University nor the names of its contributors
18314564Sdim *    may be used to endorse or promote products derived from this software
19321369Sdim *    without specific prior written permission.
20321369Sdim *
21321369Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22321369Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23292932Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24314564Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25314564Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26292932Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27314564Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28314564Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29292932Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30314564Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31314564Sdim * SUCH DAMAGE.
32314564Sdim */
33292932Sdim
34314564Sdim#ifndef lint
35314564Sdim#if 0
36314564Sdimstatic char sccsid[] = "@(#)announce.c	8.3 (Berkeley) 4/28/95";
37292932Sdim#endif
38314564Sdimstatic const char rcsid[] =
39314564Sdim  "$FreeBSD: head/libexec/talkd/announce.c 177626 2008-03-26 07:32:08Z brueffer $";
40292932Sdim#endif /* not lint */
41321369Sdim
42321369Sdim#include <sys/types.h>
43321369Sdim#include <sys/uio.h>
44321369Sdim#include <sys/stat.h>
45321369Sdim#include <sys/time.h>
46321369Sdim#include <sys/wait.h>
47321369Sdim#include <sys/socket.h>
48321369Sdim
49353358Sdim#include <protocols/talkd.h>
50353358Sdim
51321369Sdim#include <errno.h>
52321369Sdim#include <paths.h>
53321369Sdim#include <stdio.h>
54321369Sdim#include <stdlib.h>
55321369Sdim#include <string.h>
56321369Sdim#include <syslog.h>
57314564Sdim#include <unistd.h>
58292932Sdim#include <vis.h>
59292932Sdim
60292932Sdim#include "ttymsg.h"
61#include "extern.h"
62
63extern char hostname[];
64
65/*
66 * Announce an invitation to talk.
67 */
68
69/*
70 * See if the user is accepting messages. If so, announce that
71 * a talk is requested.
72 */
73int
74announce(CTL_MSG *request, const char *remote_machine)
75{
76	char full_tty[32];
77	struct stat stbuf;
78
79	(void)snprintf(full_tty, sizeof(full_tty),
80	    "%s%s", _PATH_DEV, request->r_tty);
81	if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&020) == 0)
82		return (PERMISSION_DENIED);
83	return (print_mesg(request->r_tty, request, remote_machine));
84}
85
86#define max(a,b) ( (a) > (b) ? (a) : (b) )
87#define N_LINES 5
88#define N_CHARS 256
89
90/*
91 * Build a block of characters containing the message.
92 * It is sent blank filled and in a single block to
93 * try to keep the message in one piece if the recipient
94 * in vi at the time
95 */
96int
97print_mesg(const char *tty, CTL_MSG *request,
98    const char *remote_machine)
99{
100	struct timeval now;
101	time_t clock_sec;
102	struct timezone zone;
103	struct tm *localclock;
104	struct iovec iovec;
105	char line_buf[N_LINES][N_CHARS];
106	int sizes[N_LINES];
107	char big_buf[N_LINES*N_CHARS];
108	char *bptr, *lptr, *vis_user;
109	int i, j, max_size;
110
111	i = 0;
112	max_size = 0;
113	gettimeofday(&now, &zone);
114	clock_sec = now.tv_sec;
115	localclock = localtime(&clock_sec);
116	(void)snprintf(line_buf[i], N_CHARS, " ");
117	sizes[i] = strlen(line_buf[i]);
118	max_size = max(max_size, sizes[i]);
119	i++;
120	(void)snprintf(line_buf[i], N_CHARS,
121		"Message from Talk_Daemon@%s at %d:%02d on %d/%.2d/%.2d ...",
122		hostname, localclock->tm_hour , localclock->tm_min,
123		localclock->tm_year + 1900, localclock->tm_mon + 1,
124		localclock->tm_mday);
125	sizes[i] = strlen(line_buf[i]);
126	max_size = max(max_size, sizes[i]);
127	i++;
128
129	vis_user = malloc(strlen(request->l_name) * 4 + 1);
130	strvis(vis_user, request->l_name, VIS_CSTYLE);
131	(void)snprintf(line_buf[i], N_CHARS,
132	    "talk: connection requested by %s@%s", vis_user, remote_machine);
133	sizes[i] = strlen(line_buf[i]);
134	max_size = max(max_size, sizes[i]);
135	i++;
136	(void)snprintf(line_buf[i], N_CHARS, "talk: respond with:  talk %s@%s",
137	    vis_user, remote_machine);
138	sizes[i] = strlen(line_buf[i]);
139	max_size = max(max_size, sizes[i]);
140	i++;
141	(void)snprintf(line_buf[i], N_CHARS, " ");
142	sizes[i] = strlen(line_buf[i]);
143	max_size = max(max_size, sizes[i]);
144	i++;
145	bptr = big_buf;
146	*bptr++ = '\007'; /* send something to wake them up */
147	*bptr++ = '\r';	/* add a \r in case of raw mode */
148	*bptr++ = '\n';
149	for (i = 0; i < N_LINES; i++) {
150		/* copy the line into the big buffer */
151		lptr = line_buf[i];
152		while (*lptr != '\0')
153			*(bptr++) = *(lptr++);
154		/* pad out the rest of the lines with blanks */
155		for (j = sizes[i]; j < max_size + 2; j++)
156			*(bptr++) = ' ';
157		*(bptr++) = '\r';	/* add a \r in case of raw mode */
158		*(bptr++) = '\n';
159	}
160	*bptr = '\0';
161	iovec.iov_base = big_buf;
162	iovec.iov_len = bptr - big_buf;
163	/*
164	 * we choose a timeout of RING_WAIT-5 seconds so that we don't
165	 * stack up processes trying to write messages to a tty
166	 * that is permanently blocked.
167	 */
168	if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)
169		return (FAILED);
170
171	return (SUCCESS);
172}
173