168349Sobrien/*	$NetBSD: announce.c,v 1.24 2009/03/16 01:04:32 lukem Exp $	*/
2290152Sdelphij
368349Sobrien/*
468349Sobrien * Copyright (c) 1983, 1993
568349Sobrien *	The Regents of the University of California.  All rights reserved.
668349Sobrien *
768349Sobrien * Redistribution and use in source and binary forms, with or without
868349Sobrien * modification, are permitted provided that the following conditions
968349Sobrien * are met:
1068349Sobrien * 1. Redistributions of source code must retain the above copyright
1168349Sobrien *    notice, this list of conditions and the following disclaimer.
1268349Sobrien * 2. Redistributions in binary form must reproduce the above copyright
13186690Sobrien *    notice, this list of conditions and the following disclaimer in the
14191736Sobrien *    documentation and/or other materials provided with the distribution.
1568349Sobrien * 3. Neither the name of the University nor the names of its contributors
1668349Sobrien *    may be used to endorse or promote products derived from this software
1768349Sobrien *    without specific prior written permission.
1868349Sobrien *
19103373Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20103373Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21103373Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22226048Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2368349Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24186690Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25267843Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26103373Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27103373Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28103373Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29159764Sobrien * SUCH DAMAGE.
30103373Sobrien */
31103373Sobrien
32103373Sobrien#include <sys/cdefs.h>
33159764Sobrien#ifndef lint
34267843Sdelphij#if 0
35267843Sdelphijstatic char sccsid[] = "@(#)announce.c	8.3 (Berkeley) 4/28/95";
36267843Sdelphij#else
37267843Sdelphij__RCSID("$NetBSD: announce.c,v 1.24 2009/03/16 01:04:32 lukem Exp $");
38159764Sobrien#endif
39103373Sobrien#endif /* not lint */
40103373Sobrien
41103373Sobrien#include <sys/types.h>
42159764Sobrien#include <sys/uio.h>
43103373Sobrien#include <sys/stat.h>
44159764Sobrien#include <sys/time.h>
45103373Sobrien#include <sys/wait.h>
46159764Sobrien#include <sys/socket.h>
47159764Sobrien#include <protocols/talkd.h>
48159764Sobrien#include <errno.h>
49159764Sobrien#include <syslog.h>
50159764Sobrien#include <unistd.h>
51159764Sobrien#include <stdio.h>
5268349Sobrien#include <stdlib.h>
5368349Sobrien#include <string.h>
5468349Sobrien#include <time.h>
55186690Sobrien#include <vis.h>
5668349Sobrien#include <paths.h>
5768349Sobrien#include <util.h>
5868349Sobrien#include "extern.h"
59133359Sobrien
60133359Sobrienextern char hostname[];
61186690Sobrien
6268349Sobrien/*
6368349Sobrien * Announce an invitation to talk.
6468349Sobrien */
6568349Sobrien
6668349Sobrien/*
67186690Sobrien * See if the user is accepting messages. If so, announce that
6868349Sobrien * a talk is requested.
6968349Sobrien */
7068349Sobrienint
71186690Sobrienannounce(CTL_MSG *request, const char *remote_machine)
7268349Sobrien{
73186690Sobrien	char full_tty[32];
7468349Sobrien	struct stat stbuf;
7568349Sobrien
7668349Sobrien	(void)snprintf(full_tty, sizeof(full_tty),
77186690Sobrien	    "%s%s", _PATH_DEV, request->r_tty);
7868349Sobrien	if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&S_IWGRP) == 0)
7968349Sobrien		return (PERMISSION_DENIED);
80191736Sobrien	return (print_mesg(request->r_tty, request, remote_machine));
81191736Sobrien}
82191736Sobrien
83191736Sobrien#define max(a, b) ((a) > (b) ? (a) : (b))
84191736Sobrien#define N_LINES 5
8568349Sobrien#define N_CHARS 256
8668349Sobrien
8768349Sobrien/*
8868349Sobrien * Build a block of characters containing the message.
8968349Sobrien * It is sent blank filled and in a single block to
9068349Sobrien * try to keep the message in one piece if the recipient
9168349Sobrien * is in vi at the time.
9268349Sobrien */
9368349Sobrienint
9468349Sobrienprint_mesg(const char *tty, CTL_MSG *request, const char *remote_machine)
9568349Sobrien{
9668349Sobrien	struct timeval clocktv;
9768349Sobrien	time_t clocktime;
9868349Sobrien	struct tm *localclock;
9968349Sobrien	struct iovec iovec;
10068349Sobrien	char line_buf[N_LINES][N_CHARS];
10168349Sobrien	int sizes[N_LINES];
10268349Sobrien	char big_buf[(N_LINES + 1) * N_CHARS];
10368349Sobrien	char *bptr, *lptr, vis_user[sizeof(request->l_name) * 4];
10468349Sobrien	int i, j, max_size;
10568349Sobrien
10668349Sobrien	i = 0;
10768349Sobrien	max_size = 0;
10868349Sobrien	(void)gettimeofday(&clocktv, NULL);
10968349Sobrien	clocktime = clocktv.tv_sec;
11068349Sobrien	localclock = localtime(&clocktime);
11168349Sobrien	(void)snprintf(line_buf[i], N_CHARS, " ");
11268349Sobrien	sizes[i] = strlen(line_buf[i]);
11368349Sobrien	max_size = max(max_size, sizes[i]);
11468349Sobrien	i++;
11568349Sobrien
11668349Sobrien	(void)snprintf(line_buf[i], N_CHARS,
11768349Sobrien	    "Message from Talk_Daemon@%s at %d:%02d ...",
11868349Sobrien	    hostname, localclock->tm_hour, localclock->tm_min);
119186690Sobrien	sizes[i] = strlen(line_buf[i]);
120186690Sobrien	max_size = max(max_size, sizes[i]);
121186690Sobrien	i++;
122186690Sobrien	if (strlen(request->l_name) + 1 > sizeof(vis_user) / 4)
123186690Sobrien		return (FAILED);
124186690Sobrien	strvis(vis_user, request->l_name, VIS_CSTYLE);
125186690Sobrien	(void)snprintf(line_buf[i], N_CHARS,
126186690Sobrien	    "talk: connection requested by %s@%s.", vis_user, remote_machine);
127186690Sobrien	sizes[i] = strlen(line_buf[i]);
128186690Sobrien	max_size = max(max_size, sizes[i]);
129186690Sobrien	i++;
13068349Sobrien	(void)snprintf(line_buf[i], N_CHARS,
13168349Sobrien	    "talk: respond with:  talk %s@%s", vis_user, remote_machine);
13268349Sobrien	sizes[i] = strlen(line_buf[i]);
13368349Sobrien	max_size = max(max_size, sizes[i]);
13468349Sobrien	i++;
13568349Sobrien	(void)snprintf(line_buf[i], N_CHARS, " ");
13668349Sobrien	sizes[i] = strlen(line_buf[i]);
13768349Sobrien	max_size = max(max_size, sizes[i]);
13868349Sobrien	i++;
13968349Sobrien	bptr = big_buf;
14068349Sobrien	*bptr++ = '\a';			/* send something to wake them up */
14168349Sobrien	*bptr++ = '\r';			/* add a \r in case of raw mode */
14268349Sobrien	*bptr++ = '\n';
14368349Sobrien	for (i = 0; i < N_LINES; i++) {
14468349Sobrien		/* copy the line into the big buffer */
14568349Sobrien		lptr = line_buf[i];
14668349Sobrien		while (*lptr != '\0' && lptr < (line_buf[i] + N_CHARS))
14768349Sobrien			*(bptr++) = *(lptr++);
14868349Sobrien		/* pad out the rest of the lines with blanks */
14968349Sobrien		for (j = sizes[i]; j < max_size + 2; j++)
15068349Sobrien			*(bptr++) = ' ';
15168349Sobrien		*(bptr++) = '\r';	/* add a \r in case of raw mode */
15268349Sobrien		*(bptr++) = '\n';
15368349Sobrien	}
15468349Sobrien	*bptr = '\0';
15568349Sobrien	iovec.iov_base = big_buf;
15668349Sobrien	iovec.iov_len = bptr - big_buf;
15768349Sobrien	/*
15868349Sobrien	 * we choose a timeout of RING_WAIT-5 seconds so that we don't
15968349Sobrien	 * stack up processes trying to write messages to a tty
16068349Sobrien	 * that is permanently blocked.
16168349Sobrien	 */
16268349Sobrien	if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)
16368349Sobrien		return (FAILED);
16468349Sobrien
16568349Sobrien	return (SUCCESS);
16668349Sobrien}
16768349Sobrien