Home | History | Annotate | Line # | Download | only in talkd
      1  1.25     lukem /*	$NetBSD: announce.c,v 1.25 2009/03/16 01:13:38 lukem Exp $	*/
      2   1.7  christos 
      3   1.1       cgd /*
      4   1.7  christos  * Copyright (c) 1983, 1993
      5   1.7  christos  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.21       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.7  christos #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.7  christos #if 0
     35   1.7  christos static char sccsid[] = "@(#)announce.c	8.3 (Berkeley) 4/28/95";
     36   1.7  christos #else
     37  1.25     lukem __RCSID("$NetBSD: announce.c,v 1.25 2009/03/16 01:13:38 lukem Exp $");
     38   1.7  christos #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #include <sys/types.h>
     42   1.7  christos #include <sys/uio.h>
     43   1.1       cgd #include <sys/stat.h>
     44   1.1       cgd #include <sys/time.h>
     45   1.1       cgd #include <sys/wait.h>
     46   1.1       cgd #include <sys/socket.h>
     47   1.1       cgd #include <protocols/talkd.h>
     48   1.1       cgd #include <errno.h>
     49   1.1       cgd #include <syslog.h>
     50   1.1       cgd #include <unistd.h>
     51   1.1       cgd #include <stdio.h>
     52   1.4       cgd #include <stdlib.h>
     53   1.1       cgd #include <string.h>
     54   1.9    kleink #include <time.h>
     55   1.3     glass #include <vis.h>
     56   1.1       cgd #include <paths.h>
     57   1.8  christos #include <util.h>
     58   1.8  christos #include "extern.h"
     59   1.1       cgd 
     60   1.1       cgd extern char hostname[];
     61   1.1       cgd 
     62   1.1       cgd /*
     63   1.1       cgd  * Announce an invitation to talk.
     64   1.1       cgd  */
     65   1.1       cgd 
     66   1.1       cgd /*
     67  1.15     enami  * See if the user is accepting messages. If so, announce that
     68   1.1       cgd  * a talk is requested.
     69   1.1       cgd  */
     70   1.8  christos int
     71  1.25     lukem announce(CTL_MSG *request, const char *remote_machine)
     72   1.1       cgd {
     73   1.1       cgd 	char full_tty[32];
     74   1.1       cgd 	struct stat stbuf;
     75   1.1       cgd 
     76   1.7  christos 	(void)snprintf(full_tty, sizeof(full_tty),
     77   1.7  christos 	    "%s%s", _PATH_DEV, request->r_tty);
     78  1.10       mrg 	if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&S_IWGRP) == 0)
     79   1.1       cgd 		return (PERMISSION_DENIED);
     80   1.8  christos 	return (print_mesg(request->r_tty, request, remote_machine));
     81   1.1       cgd }
     82   1.1       cgd 
     83  1.15     enami #define max(a, b) ((a) > (b) ? (a) : (b))
     84   1.1       cgd #define N_LINES 5
     85   1.7  christos #define N_CHARS 256
     86   1.1       cgd 
     87   1.1       cgd /*
     88  1.15     enami  * Build a block of characters containing the message.
     89   1.1       cgd  * It is sent blank filled and in a single block to
     90   1.1       cgd  * try to keep the message in one piece if the recipient
     91  1.22  dholland  * is in vi at the time.
     92   1.1       cgd  */
     93   1.8  christos int
     94  1.25     lukem print_mesg(const char *tty, CTL_MSG *request, const char *remote_machine)
     95   1.1       cgd {
     96  1.25     lukem 	struct timeval clocktv;
     97   1.5       cgd 	time_t clocktime;
     98   1.1       cgd 	struct tm *localclock;
     99   1.7  christos 	struct iovec iovec;
    100   1.1       cgd 	char line_buf[N_LINES][N_CHARS];
    101   1.1       cgd 	int sizes[N_LINES];
    102  1.13        tv 	char big_buf[(N_LINES + 1) * N_CHARS];
    103  1.20    itojun 	char *bptr, *lptr, vis_user[sizeof(request->l_name) * 4];
    104   1.1       cgd 	int i, j, max_size;
    105   1.1       cgd 
    106   1.1       cgd 	i = 0;
    107   1.1       cgd 	max_size = 0;
    108  1.25     lukem 	(void)gettimeofday(&clocktv, NULL);
    109  1.25     lukem 	clocktime = clocktv.tv_sec;
    110   1.5       cgd 	localclock = localtime(&clocktime);
    111   1.6       rat 	(void)snprintf(line_buf[i], N_CHARS, " ");
    112   1.1       cgd 	sizes[i] = strlen(line_buf[i]);
    113   1.1       cgd 	max_size = max(max_size, sizes[i]);
    114   1.1       cgd 	i++;
    115  1.10       mrg 
    116  1.15     enami 	(void)snprintf(line_buf[i], N_CHARS,
    117   1.6       rat 	    "Message from Talk_Daemon@%s at %d:%02d ...",
    118  1.15     enami 	    hostname, localclock->tm_hour, localclock->tm_min);
    119   1.1       cgd 	sizes[i] = strlen(line_buf[i]);
    120   1.1       cgd 	max_size = max(max_size, sizes[i]);
    121   1.1       cgd 	i++;
    122  1.20    itojun 	if (strlen(request->l_name) + 1 > sizeof(vis_user) / 4)
    123  1.17    itojun 		return (FAILED);
    124   1.3     glass 	strvis(vis_user, request->l_name, VIS_CSTYLE);
    125  1.15     enami 	(void)snprintf(line_buf[i], N_CHARS,
    126   1.6       rat 	    "talk: connection requested by %s@%s.", vis_user, remote_machine);
    127   1.1       cgd 	sizes[i] = strlen(line_buf[i]);
    128   1.1       cgd 	max_size = max(max_size, sizes[i]);
    129   1.1       cgd 	i++;
    130   1.6       rat 	(void)snprintf(line_buf[i], N_CHARS,
    131   1.6       rat 	    "talk: respond with:  talk %s@%s", vis_user, remote_machine);
    132   1.1       cgd 	sizes[i] = strlen(line_buf[i]);
    133   1.1       cgd 	max_size = max(max_size, sizes[i]);
    134   1.1       cgd 	i++;
    135   1.6       rat 	(void)snprintf(line_buf[i], N_CHARS, " ");
    136   1.1       cgd 	sizes[i] = strlen(line_buf[i]);
    137   1.1       cgd 	max_size = max(max_size, sizes[i]);
    138   1.1       cgd 	i++;
    139   1.1       cgd 	bptr = big_buf;
    140  1.15     enami 	*bptr++ = '\a';			/* send something to wake them up */
    141  1.15     enami 	*bptr++ = '\r';			/* add a \r in case of raw mode */
    142   1.1       cgd 	*bptr++ = '\n';
    143   1.1       cgd 	for (i = 0; i < N_LINES; i++) {
    144   1.1       cgd 		/* copy the line into the big buffer */
    145   1.1       cgd 		lptr = line_buf[i];
    146  1.11       mrg 		while (*lptr != '\0' && lptr < (line_buf[i] + N_CHARS))
    147   1.1       cgd 			*(bptr++) = *(lptr++);
    148   1.1       cgd 		/* pad out the rest of the lines with blanks */
    149   1.1       cgd 		for (j = sizes[i]; j < max_size + 2; j++)
    150   1.1       cgd 			*(bptr++) = ' ';
    151   1.1       cgd 		*(bptr++) = '\r';	/* add a \r in case of raw mode */
    152   1.1       cgd 		*(bptr++) = '\n';
    153   1.1       cgd 	}
    154   1.1       cgd 	*bptr = '\0';
    155   1.7  christos 	iovec.iov_base = big_buf;
    156   1.7  christos 	iovec.iov_len = bptr - big_buf;
    157   1.7  christos 	/*
    158   1.7  christos 	 * we choose a timeout of RING_WAIT-5 seconds so that we don't
    159   1.7  christos 	 * stack up processes trying to write messages to a tty
    160   1.7  christos 	 * that is permanently blocked.
    161   1.7  christos 	 */
    162   1.7  christos 	if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)
    163   1.7  christos 		return (FAILED);
    164   1.7  christos 
    165   1.7  christos 	return (SUCCESS);
    166   1.1       cgd }
    167