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