announce.c revision 79674
1252867Sdelphij/* 2252867Sdelphij * Copyright (c) 1983, 1993 3252867Sdelphij * The Regents of the University of California. All rights reserved. 4252867Sdelphij * 5252867Sdelphij * Redistribution and use in source and binary forms, with or without 6252867Sdelphij * modification, are permitted provided that the following conditions 7252867Sdelphij * are met: 8252867Sdelphij * 1. Redistributions of source code must retain the above copyright 9252867Sdelphij * notice, this list of conditions and the following disclaimer. 10252867Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 11252867Sdelphij * notice, this list of conditions and the following disclaimer in the 12252867Sdelphij * documentation and/or other materials provided with the distribution. 13252867Sdelphij * 3. All advertising materials mentioning features or use of this software 14252867Sdelphij * must display the following acknowledgement: 15252867Sdelphij * This product includes software developed by the University of 16252867Sdelphij * California, Berkeley and its contributors. 17252867Sdelphij * 4. Neither the name of the University nor the names of its contributors 18252867Sdelphij * may be used to endorse or promote products derived from this software 19252867Sdelphij * without specific prior written permission. 20252867Sdelphij * 21252867Sdelphij * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22252867Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23252867Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24252867Sdelphij * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25252867Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26252867Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27252867Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28252867Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29252867Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30252867Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31252867Sdelphij * SUCH DAMAGE. 32252867Sdelphij */ 33252867Sdelphij 34252867Sdelphij#ifndef lint 35252867Sdelphij#if 0 36252867Sdelphijstatic char sccsid[] = "@(#)announce.c 8.3 (Berkeley) 4/28/95"; 37252867Sdelphij#endif 38252867Sdelphijstatic const char rcsid[] = 39252867Sdelphij "$FreeBSD: head/libexec/talkd/announce.c 79674 2001-07-13 15:07:06Z brian $"; 40252867Sdelphij#endif /* not lint */ 41252867Sdelphij 42252867Sdelphij#include <sys/types.h> 43252867Sdelphij#include <sys/uio.h> 44252867Sdelphij#include <sys/stat.h> 45252867Sdelphij#include <sys/time.h> 46252867Sdelphij#include <sys/wait.h> 47252867Sdelphij#include <sys/socket.h> 48252867Sdelphij 49252867Sdelphij#include <protocols/talkd.h> 50252867Sdelphij 51252867Sdelphij#include <errno.h> 52252867Sdelphij#include <paths.h> 53252867Sdelphij#include <stdio.h> 54252867Sdelphij#include <stdlib.h> 55252867Sdelphij#include <string.h> 56252867Sdelphij#include <syslog.h> 57252867Sdelphij#include <unistd.h> 58252867Sdelphij#include <vis.h> 59252867Sdelphij 60252867Sdelphijextern char hostname[]; 61252867Sdelphij 62252867Sdelphijint print_mesg __P((char *, FILE *, CTL_MSG *, char *)); 63252867Sdelphijchar *ttymsg __P((struct iovec *, int, char *, int)); 64252867Sdelphij 65252867Sdelphij/* 66252867Sdelphij * Announce an invitation to talk. 67252867Sdelphij */ 68252867Sdelphij 69252867Sdelphij/* 70252867Sdelphij * See if the user is accepting messages. If so, announce that 71252867Sdelphij * a talk is requested. 72252867Sdelphij */ 73252867Sdelphijint 74252867Sdelphijannounce(request, remote_machine) 75252867Sdelphij CTL_MSG *request; 76252867Sdelphij char *remote_machine; 77252867Sdelphij{ 78252867Sdelphij char full_tty[32]; 79252867Sdelphij FILE *tf; 80252867Sdelphij struct stat stbuf; 81252867Sdelphij 82252867Sdelphij (void)snprintf(full_tty, sizeof(full_tty), 83252867Sdelphij "%s%s", _PATH_DEV, request->r_tty); 84252867Sdelphij if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&020) == 0) 85252867Sdelphij return (PERMISSION_DENIED); 86252867Sdelphij return (print_mesg(request->r_tty, tf, request, remote_machine)); 87252867Sdelphij} 88252867Sdelphij 89252867Sdelphij#define max(a,b) ( (a) > (b) ? (a) : (b) ) 90252867Sdelphij#define N_LINES 5 91252867Sdelphij#define N_CHARS 256 92252867Sdelphij 93252867Sdelphij/* 94252867Sdelphij * Build a block of characters containing the message. 95252867Sdelphij * It is sent blank filled and in a single block to 96252867Sdelphij * try to keep the message in one piece if the recipient 97252867Sdelphij * in in vi at the time 98252867Sdelphij */ 99252867Sdelphijint 100252867Sdelphijprint_mesg(tty, tf, request, remote_machine) 101252867Sdelphij char *tty; 102252867Sdelphij FILE *tf; 103252867Sdelphij CTL_MSG *request; 104252867Sdelphij char *remote_machine; 105252867Sdelphij{ 106252867Sdelphij struct timeval clock; 107252867Sdelphij time_t clock_sec; 108252867Sdelphij struct timezone zone; 109 struct tm *localtime(); 110 struct tm *localclock; 111 struct iovec iovec; 112 char line_buf[N_LINES][N_CHARS]; 113 int sizes[N_LINES]; 114 char big_buf[N_LINES*N_CHARS]; 115 char *bptr, *lptr, *vis_user; 116 int i, j, max_size; 117 118 i = 0; 119 max_size = 0; 120 gettimeofday(&clock, &zone); 121 clock_sec = clock.tv_sec; 122 localclock = localtime(&clock_sec); 123 (void)snprintf(line_buf[i], N_CHARS, " "); 124 sizes[i] = strlen(line_buf[i]); 125 max_size = max(max_size, sizes[i]); 126 i++; 127 (void)snprintf(line_buf[i], N_CHARS, 128 "Message from Talk_Daemon@%s at %d:%02d on %d/%.2d/%.2d ...", 129 hostname, localclock->tm_hour , localclock->tm_min, 130 localclock->tm_year + 1900, localclock->tm_mon + 1, 131 localclock->tm_mday); 132 sizes[i] = strlen(line_buf[i]); 133 max_size = max(max_size, sizes[i]); 134 i++; 135 136 vis_user = malloc(strlen(request->l_name) * 4 + 1); 137 strvis(vis_user, request->l_name, VIS_CSTYLE); 138 (void)snprintf(line_buf[i], N_CHARS, 139 "talk: connection requested by %s@%s", vis_user, remote_machine); 140 sizes[i] = strlen(line_buf[i]); 141 max_size = max(max_size, sizes[i]); 142 i++; 143 (void)snprintf(line_buf[i], N_CHARS, "talk: respond with: talk %s@%s", 144 vis_user, remote_machine); 145 sizes[i] = strlen(line_buf[i]); 146 max_size = max(max_size, sizes[i]); 147 i++; 148 (void)snprintf(line_buf[i], N_CHARS, " "); 149 sizes[i] = strlen(line_buf[i]); 150 max_size = max(max_size, sizes[i]); 151 i++; 152 bptr = big_buf; 153 *bptr++ = '\007'; /* send something to wake them up */ 154 *bptr++ = '\r'; /* add a \r in case of raw mode */ 155 *bptr++ = '\n'; 156 for (i = 0; i < N_LINES; i++) { 157 /* copy the line into the big buffer */ 158 lptr = line_buf[i]; 159 while (*lptr != '\0') 160 *(bptr++) = *(lptr++); 161 /* pad out the rest of the lines with blanks */ 162 for (j = sizes[i]; j < max_size + 2; j++) 163 *(bptr++) = ' '; 164 *(bptr++) = '\r'; /* add a \r in case of raw mode */ 165 *(bptr++) = '\n'; 166 } 167 *bptr = '\0'; 168 iovec.iov_base = big_buf; 169 iovec.iov_len = bptr - big_buf; 170 /* 171 * we choose a timeout of RING_WAIT-5 seconds so that we don't 172 * stack up processes trying to write messages to a tty 173 * that is permanently blocked. 174 */ 175 if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL) 176 return (FAILED); 177 178 return (SUCCESS); 179} 180