announce.c revision 112998
11592Srgrimes/* 21592Srgrimes * Copyright (c) 1983, 1993 31592Srgrimes * The Regents of the University of California. All rights reserved. 41592Srgrimes * 51592Srgrimes * Redistribution and use in source and binary forms, with or without 61592Srgrimes * modification, are permitted provided that the following conditions 71592Srgrimes * are met: 81592Srgrimes * 1. Redistributions of source code must retain the above copyright 91592Srgrimes * notice, this list of conditions and the following disclaimer. 101592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111592Srgrimes * notice, this list of conditions and the following disclaimer in the 121592Srgrimes * documentation and/or other materials provided with the distribution. 131592Srgrimes * 3. All advertising materials mentioning features or use of this software 141592Srgrimes * must display the following acknowledgement: 151592Srgrimes * This product includes software developed by the University of 161592Srgrimes * California, Berkeley and its contributors. 171592Srgrimes * 4. Neither the name of the University nor the names of its contributors 181592Srgrimes * may be used to endorse or promote products derived from this software 191592Srgrimes * without specific prior written permission. 201592Srgrimes * 211592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241592Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311592Srgrimes * SUCH DAMAGE. 321592Srgrimes */ 331592Srgrimes 341592Srgrimes#ifndef lint 3531491Scharnier#if 0 3621838Spststatic char sccsid[] = "@(#)announce.c 8.3 (Berkeley) 4/28/95"; 3731491Scharnier#endif 3831491Scharnierstatic const char rcsid[] = 3950476Speter "$FreeBSD: head/libexec/talkd/announce.c 112998 2003-04-03 05:13:27Z jmallett $"; 401592Srgrimes#endif /* not lint */ 411592Srgrimes 421592Srgrimes#include <sys/types.h> 431592Srgrimes#include <sys/uio.h> 441592Srgrimes#include <sys/stat.h> 451592Srgrimes#include <sys/time.h> 461592Srgrimes#include <sys/wait.h> 471592Srgrimes#include <sys/socket.h> 4821838Spst 491592Srgrimes#include <protocols/talkd.h> 5021838Spst 511592Srgrimes#include <errno.h> 5221838Spst#include <paths.h> 5321838Spst#include <stdio.h> 5421838Spst#include <stdlib.h> 5521838Spst#include <string.h> 561592Srgrimes#include <syslog.h> 571592Srgrimes#include <unistd.h> 5821838Spst#include <vis.h> 591592Srgrimes 6083244Sdd#include "ttymsg.h" 6190261Simp#include "extern.h" 6283244Sdd 631592Srgrimesextern char hostname[]; 641592Srgrimes 651592Srgrimes/* 661592Srgrimes * Announce an invitation to talk. 671592Srgrimes */ 688870Srgrimes 691592Srgrimes/* 708870Srgrimes * See if the user is accepting messages. If so, announce that 711592Srgrimes * a talk is requested. 721592Srgrimes */ 7331491Scharnierint 7490261Simpannounce(CTL_MSG *request, const char *remote_machine) 751592Srgrimes{ 761592Srgrimes char full_tty[32]; 771592Srgrimes struct stat stbuf; 781592Srgrimes 791592Srgrimes (void)snprintf(full_tty, sizeof(full_tty), 801592Srgrimes "%s%s", _PATH_DEV, request->r_tty); 811592Srgrimes if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&020) == 0) 821592Srgrimes return (PERMISSION_DENIED); 83112998Sjmallett return (print_mesg(request->r_tty, request, remote_machine)); 841592Srgrimes} 851592Srgrimes 861592Srgrimes#define max(a,b) ( (a) > (b) ? (a) : (b) ) 871592Srgrimes#define N_LINES 5 8821838Spst#define N_CHARS 256 891592Srgrimes 901592Srgrimes/* 918870Srgrimes * Build a block of characters containing the message. 921592Srgrimes * It is sent blank filled and in a single block to 931592Srgrimes * try to keep the message in one piece if the recipient 941592Srgrimes * in in vi at the time 951592Srgrimes */ 9631491Scharnierint 97112998Sjmallettprint_mesg(const char *tty, CTL_MSG *request, 9890261Simp const char *remote_machine) 991592Srgrimes{ 100112998Sjmallett struct timeval now; 10137262Sbde time_t clock_sec; 1021592Srgrimes struct timezone zone; 1031592Srgrimes struct tm *localclock; 1041592Srgrimes struct iovec iovec; 1051592Srgrimes char line_buf[N_LINES][N_CHARS]; 1061592Srgrimes int sizes[N_LINES]; 1071592Srgrimes char big_buf[N_LINES*N_CHARS]; 10821838Spst char *bptr, *lptr, *vis_user; 1091592Srgrimes int i, j, max_size; 1101592Srgrimes 1111592Srgrimes i = 0; 1121592Srgrimes max_size = 0; 113112998Sjmallett gettimeofday(&now, &zone); 114112998Sjmallett clock_sec = now.tv_sec; 11537262Sbde localclock = localtime(&clock_sec); 11621838Spst (void)snprintf(line_buf[i], N_CHARS, " "); 1171592Srgrimes sizes[i] = strlen(line_buf[i]); 1181592Srgrimes max_size = max(max_size, sizes[i]); 1191592Srgrimes i++; 12021838Spst (void)snprintf(line_buf[i], N_CHARS, 12177862Sdd "Message from Talk_Daemon@%s at %d:%02d on %d/%.2d/%.2d ...", 12277862Sdd hostname, localclock->tm_hour , localclock->tm_min, 12379674Sbrian localclock->tm_year + 1900, localclock->tm_mon + 1, 12477862Sdd localclock->tm_mday); 1251592Srgrimes sizes[i] = strlen(line_buf[i]); 1261592Srgrimes max_size = max(max_size, sizes[i]); 1271592Srgrimes i++; 12821838Spst 12921838Spst vis_user = malloc(strlen(request->l_name) * 4 + 1); 13021838Spst strvis(vis_user, request->l_name, VIS_CSTYLE); 13121838Spst (void)snprintf(line_buf[i], N_CHARS, 13221838Spst "talk: connection requested by %s@%s", vis_user, remote_machine); 1331592Srgrimes sizes[i] = strlen(line_buf[i]); 1341592Srgrimes max_size = max(max_size, sizes[i]); 1351592Srgrimes i++; 13621838Spst (void)snprintf(line_buf[i], N_CHARS, "talk: respond with: talk %s@%s", 13721838Spst vis_user, remote_machine); 1381592Srgrimes sizes[i] = strlen(line_buf[i]); 1391592Srgrimes max_size = max(max_size, sizes[i]); 1401592Srgrimes i++; 14121838Spst (void)snprintf(line_buf[i], N_CHARS, " "); 1421592Srgrimes sizes[i] = strlen(line_buf[i]); 1431592Srgrimes max_size = max(max_size, sizes[i]); 1441592Srgrimes i++; 1451592Srgrimes bptr = big_buf; 1461801Sphk *bptr++ = '\007'; /* send something to wake them up */ 1471592Srgrimes *bptr++ = '\r'; /* add a \r in case of raw mode */ 1481592Srgrimes *bptr++ = '\n'; 1491592Srgrimes for (i = 0; i < N_LINES; i++) { 1501592Srgrimes /* copy the line into the big buffer */ 1511592Srgrimes lptr = line_buf[i]; 1521592Srgrimes while (*lptr != '\0') 1531592Srgrimes *(bptr++) = *(lptr++); 1541592Srgrimes /* pad out the rest of the lines with blanks */ 1551592Srgrimes for (j = sizes[i]; j < max_size + 2; j++) 1561592Srgrimes *(bptr++) = ' '; 1571592Srgrimes *(bptr++) = '\r'; /* add a \r in case of raw mode */ 1581592Srgrimes *(bptr++) = '\n'; 1591592Srgrimes } 1601592Srgrimes *bptr = '\0'; 1611592Srgrimes iovec.iov_base = big_buf; 1621592Srgrimes iovec.iov_len = bptr - big_buf; 1631592Srgrimes /* 1641592Srgrimes * we choose a timeout of RING_WAIT-5 seconds so that we don't 1651592Srgrimes * stack up processes trying to write messages to a tty 1661592Srgrimes * that is permanently blocked. 1671592Srgrimes */ 1681592Srgrimes if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL) 1691592Srgrimes return (FAILED); 1701592Srgrimes 1711592Srgrimes return (SUCCESS); 1721592Srgrimes} 173