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