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 #ifndef lint 33 #endif /* not lint */ 34 35 #include <sys/types.h> 36 #include <sys/uio.h> 37 #include <sys/stat.h> 38 #include <sys/time.h> 39 #include <sys/wait.h> 40 #include <sys/socket.h> 41 42 #include <protocols/talkd.h> 43 44 #include <errno.h> 45 #include <paths.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <syslog.h> 50 #include <unistd.h> 51 #include <vis.h> 52 53 #include "ttymsg.h" 54 #include "extern.h" 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, 89 const char *remote_machine) 90 { 91 struct timeval now; 92 time_t clock_sec; 93 struct tm *localclock; 94 struct iovec iovec; 95 char line_buf[N_LINES][N_CHARS]; 96 int sizes[N_LINES]; 97 char big_buf[N_LINES*N_CHARS]; 98 char *bptr, *lptr, *vis_user; 99 int i, j, max_size; 100 101 i = 0; 102 max_size = 0; 103 gettimeofday(&now, NULL); 104 clock_sec = now.tv_sec; 105 localclock = localtime(&clock_sec); 106 (void)snprintf(line_buf[i], N_CHARS, " "); 107 sizes[i] = strlen(line_buf[i]); 108 max_size = max(max_size, sizes[i]); 109 i++; 110 (void)snprintf(line_buf[i], N_CHARS, 111 "Message from Talk_Daemon@%s at %d:%02d on %d/%.2d/%.2d ...", 112 hostname, localclock->tm_hour , localclock->tm_min, 113 localclock->tm_year + 1900, localclock->tm_mon + 1, 114 localclock->tm_mday); 115 sizes[i] = strlen(line_buf[i]); 116 max_size = max(max_size, sizes[i]); 117 i++; 118 119 vis_user = malloc(strlen(request->l_name) * 4 + 1); 120 strvis(vis_user, request->l_name, VIS_CSTYLE); 121 (void)snprintf(line_buf[i], N_CHARS, 122 "talk: connection requested by %s@%s", vis_user, remote_machine); 123 sizes[i] = strlen(line_buf[i]); 124 max_size = max(max_size, sizes[i]); 125 i++; 126 (void)snprintf(line_buf[i], N_CHARS, "talk: respond with: talk %s@%s", 127 vis_user, remote_machine); 128 sizes[i] = strlen(line_buf[i]); 129 max_size = max(max_size, sizes[i]); 130 i++; 131 (void)snprintf(line_buf[i], N_CHARS, " "); 132 sizes[i] = strlen(line_buf[i]); 133 max_size = max(max_size, sizes[i]); 134 i++; 135 bptr = big_buf; 136 *bptr++ = '\007'; /* send something to wake them up */ 137 *bptr++ = '\r'; /* add a \r in case of raw mode */ 138 *bptr++ = '\n'; 139 for (i = 0; i < N_LINES; i++) { 140 /* copy the line into the big buffer */ 141 lptr = line_buf[i]; 142 while (*lptr != '\0') 143 *(bptr++) = *(lptr++); 144 /* pad out the rest of the lines with blanks */ 145 for (j = sizes[i]; j < max_size + 2; j++) 146 *(bptr++) = ' '; 147 *(bptr++) = '\r'; /* add a \r in case of raw mode */ 148 *(bptr++) = '\n'; 149 } 150 *bptr = '\0'; 151 iovec.iov_base = big_buf; 152 iovec.iov_len = bptr - big_buf; 153 /* 154 * we choose a timeout of RING_WAIT-5 seconds so that we don't 155 * stack up processes trying to write messages to a tty 156 * that is permanently blocked. 157 */ 158 if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL) 159 return (FAILED); 160 161 return (SUCCESS); 162 } 163