1 /* $NetBSD: announce.c,v 1.21 2003/08/07 09:46:50 agc Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. 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 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)announce.c 8.3 (Berkeley) 4/28/95"; 36 #else 37 __RCSID("$NetBSD: announce.c,v 1.21 2003/08/07 09:46:50 agc Exp $"); 38 #endif 39 #endif /* not lint */ 40 41 #include <sys/types.h> 42 #include <sys/uio.h> 43 #include <sys/stat.h> 44 #include <sys/time.h> 45 #include <sys/wait.h> 46 #include <sys/socket.h> 47 #include <protocols/talkd.h> 48 #include <errno.h> 49 #include <syslog.h> 50 #include <unistd.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <time.h> 55 #include <vis.h> 56 #include <paths.h> 57 #include <util.h> 58 #include "extern.h" 59 60 extern char hostname[]; 61 62 /* 63 * Announce an invitation to talk. 64 */ 65 66 /* 67 * See if the user is accepting messages. If so, announce that 68 * a talk is requested. 69 */ 70 int 71 announce(request, remote_machine) 72 CTL_MSG *request; 73 char *remote_machine; 74 { 75 char full_tty[32]; 76 struct stat stbuf; 77 78 (void)snprintf(full_tty, sizeof(full_tty), 79 "%s%s", _PATH_DEV, request->r_tty); 80 if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&S_IWGRP) == 0) 81 return (PERMISSION_DENIED); 82 return (print_mesg(request->r_tty, request, remote_machine)); 83 } 84 85 #define max(a, b) ((a) > (b) ? (a) : (b)) 86 #define N_LINES 5 87 #define N_CHARS 256 88 89 /* 90 * Build a block of characters containing the message. 91 * It is sent blank filled and in a single block to 92 * try to keep the message in one piece if the recipient 93 * in in vi at the time 94 */ 95 int 96 print_mesg(tty, request, remote_machine) 97 char *tty; 98 CTL_MSG *request; 99 char *remote_machine; 100 { 101 struct timeval clock; 102 time_t clocktime; 103 struct timezone zone; 104 struct tm *localclock; 105 struct iovec iovec; 106 char line_buf[N_LINES][N_CHARS]; 107 int sizes[N_LINES]; 108 char big_buf[(N_LINES + 1) * N_CHARS]; 109 char *bptr, *lptr, vis_user[sizeof(request->l_name) * 4]; 110 int i, j, max_size; 111 112 i = 0; 113 max_size = 0; 114 (void)gettimeofday(&clock, &zone); 115 clocktime = clock.tv_sec; 116 localclock = localtime(&clocktime); 117 (void)snprintf(line_buf[i], N_CHARS, " "); 118 sizes[i] = strlen(line_buf[i]); 119 max_size = max(max_size, sizes[i]); 120 i++; 121 122 (void)snprintf(line_buf[i], N_CHARS, 123 "Message from Talk_Daemon@%s at %d:%02d ...", 124 hostname, localclock->tm_hour, localclock->tm_min); 125 sizes[i] = strlen(line_buf[i]); 126 max_size = max(max_size, sizes[i]); 127 i++; 128 if (strlen(request->l_name) + 1 > sizeof(vis_user) / 4) 129 return (FAILED); 130 strvis(vis_user, request->l_name, VIS_CSTYLE); 131 (void)snprintf(line_buf[i], N_CHARS, 132 "talk: connection requested by %s@%s.", 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 "talk: respond with: talk %s@%s", vis_user, remote_machine); 138 sizes[i] = strlen(line_buf[i]); 139 max_size = max(max_size, sizes[i]); 140 i++; 141 (void)snprintf(line_buf[i], N_CHARS, " "); 142 sizes[i] = strlen(line_buf[i]); 143 max_size = max(max_size, sizes[i]); 144 i++; 145 bptr = big_buf; 146 *bptr++ = '\a'; /* send something to wake them up */ 147 *bptr++ = '\r'; /* add a \r in case of raw mode */ 148 *bptr++ = '\n'; 149 for (i = 0; i < N_LINES; i++) { 150 /* copy the line into the big buffer */ 151 lptr = line_buf[i]; 152 while (*lptr != '\0' && lptr < (line_buf[i] + N_CHARS)) 153 *(bptr++) = *(lptr++); 154 /* pad out the rest of the lines with blanks */ 155 for (j = sizes[i]; j < max_size + 2; j++) 156 *(bptr++) = ' '; 157 *(bptr++) = '\r'; /* add a \r in case of raw mode */ 158 *(bptr++) = '\n'; 159 } 160 *bptr = '\0'; 161 iovec.iov_base = big_buf; 162 iovec.iov_len = bptr - big_buf; 163 /* 164 * we choose a timeout of RING_WAIT-5 seconds so that we don't 165 * stack up processes trying to write messages to a tty 166 * that is permanently blocked. 167 */ 168 if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL) 169 return (FAILED); 170 171 return (SUCCESS); 172 } 173