/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1983, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <sys/types.h>32#include <sys/uio.h>33#include <sys/stat.h>34#include <sys/time.h>35#include <sys/wait.h>36#include <sys/socket.h>3738#include <protocols/talkd.h>3940#include <errno.h>41#include <paths.h>42#include <stdio.h>43#include <stdlib.h>44#include <string.h>45#include <syslog.h>46#include <unistd.h>47#include <vis.h>4849#include "ttymsg.h"50#include "extern.h"5152/*53* Announce an invitation to talk.54*/5556/*57* See if the user is accepting messages. If so, announce that58* a talk is requested.59*/60int61announce(CTL_MSG *request, const char *remote_machine)62{63char full_tty[32];64struct stat stbuf;6566(void)snprintf(full_tty, sizeof(full_tty),67"%s%s", _PATH_DEV, request->r_tty);68if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&020) == 0)69return (PERMISSION_DENIED);70return (print_mesg(request->r_tty, request, remote_machine));71}7273#define max(a,b) ( (a) > (b) ? (a) : (b) )74#define N_LINES 575#define N_CHARS 2567677/*78* Build a block of characters containing the message.79* It is sent blank filled and in a single block to80* try to keep the message in one piece if the recipient81* in vi at the time82*/83int84print_mesg(const char *tty, CTL_MSG *request,85const char *remote_machine)86{87struct timeval now;88time_t clock_sec;89struct tm *localclock;90struct iovec iovec;91char line_buf[N_LINES][N_CHARS];92int sizes[N_LINES];93char big_buf[N_LINES*N_CHARS];94char *bptr, *lptr, *vis_user;95int i, j, max_size;9697i = 0;98max_size = 0;99gettimeofday(&now, NULL);100clock_sec = now.tv_sec;101localclock = localtime(&clock_sec);102(void)snprintf(line_buf[i], N_CHARS, " ");103sizes[i] = strlen(line_buf[i]);104max_size = max(max_size, sizes[i]);105i++;106(void)snprintf(line_buf[i], N_CHARS,107"Message from Talk_Daemon@%s at %d:%02d on %d/%.2d/%.2d ...",108hostname, localclock->tm_hour , localclock->tm_min,109localclock->tm_year + 1900, localclock->tm_mon + 1,110localclock->tm_mday);111sizes[i] = strlen(line_buf[i]);112max_size = max(max_size, sizes[i]);113i++;114115vis_user = malloc(strlen(request->l_name) * 4 + 1);116strvis(vis_user, request->l_name, VIS_CSTYLE);117(void)snprintf(line_buf[i], N_CHARS,118"talk: connection requested by %s@%s", vis_user, remote_machine);119sizes[i] = strlen(line_buf[i]);120max_size = max(max_size, sizes[i]);121i++;122(void)snprintf(line_buf[i], N_CHARS, "talk: respond with: talk %s@%s",123vis_user, remote_machine);124sizes[i] = strlen(line_buf[i]);125max_size = max(max_size, sizes[i]);126i++;127(void)snprintf(line_buf[i], N_CHARS, " ");128sizes[i] = strlen(line_buf[i]);129max_size = max(max_size, sizes[i]);130i++;131bptr = big_buf;132*bptr++ = '\007'; /* send something to wake them up */133*bptr++ = '\r'; /* add a \r in case of raw mode */134*bptr++ = '\n';135for (i = 0; i < N_LINES; i++) {136/* copy the line into the big buffer */137lptr = line_buf[i];138while (*lptr != '\0')139*(bptr++) = *(lptr++);140/* pad out the rest of the lines with blanks */141for (j = sizes[i]; j < max_size + 2; j++)142*(bptr++) = ' ';143*(bptr++) = '\r'; /* add a \r in case of raw mode */144*(bptr++) = '\n';145}146*bptr = '\0';147iovec.iov_base = big_buf;148iovec.iov_len = bptr - big_buf;149/*150* we choose a timeout of RING_WAIT-5 seconds so that we don't151* stack up processes trying to write messages to a tty152* that is permanently blocked.153*/154if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)155return (FAILED);156157return (SUCCESS);158}159160161