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