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