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