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