Home | History | Annotate | Line # | Download | only in talkd
announce.c revision 1.2
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1983 Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.2  mycroft /*static char sccsid[] = "from: @(#)announce.c	5.9 (Berkeley) 2/26/91";*/
     36  1.2  mycroft static char rcsid[] = "$Id: announce.c,v 1.2 1993/08/01 18:29:37 mycroft Exp $";
     37  1.1      cgd #endif /* not lint */
     38  1.1      cgd 
     39  1.1      cgd #include <sys/types.h>
     40  1.1      cgd #include <sys/stat.h>
     41  1.1      cgd #include <sys/time.h>
     42  1.1      cgd #include <sys/wait.h>
     43  1.1      cgd #include <sys/socket.h>
     44  1.1      cgd #include <protocols/talkd.h>
     45  1.1      cgd #include <sgtty.h>
     46  1.1      cgd #include <errno.h>
     47  1.1      cgd #include <syslog.h>
     48  1.1      cgd #include <unistd.h>
     49  1.1      cgd #include <stdio.h>
     50  1.1      cgd #include <string.h>
     51  1.1      cgd #include <paths.h>
     52  1.1      cgd 
     53  1.1      cgd extern char hostname[];
     54  1.1      cgd 
     55  1.1      cgd /*
     56  1.1      cgd  * Announce an invitation to talk.
     57  1.1      cgd  *
     58  1.1      cgd  * Because the tty driver insists on attaching a terminal-less
     59  1.1      cgd  * process to any terminal that it writes on, we must fork a child
     60  1.1      cgd  * to protect ourselves
     61  1.1      cgd  */
     62  1.1      cgd announce(request, remote_machine)
     63  1.1      cgd 	CTL_MSG *request;
     64  1.1      cgd 	char *remote_machine;
     65  1.1      cgd {
     66  1.1      cgd 	int pid, val, status;
     67  1.1      cgd 
     68  1.1      cgd 	if (pid = fork()) {
     69  1.1      cgd 		/* we are the parent, so wait for the child */
     70  1.1      cgd 		if (pid == -1)		/* the fork failed */
     71  1.1      cgd 			return (FAILED);
     72  1.1      cgd 		do {
     73  1.1      cgd 			val = wait(&status);
     74  1.1      cgd 			if (val == -1) {
     75  1.1      cgd 				if (errno == EINTR)
     76  1.1      cgd 					continue;
     77  1.1      cgd 				/* shouldn't happen */
     78  1.1      cgd 				syslog(LOG_WARNING, "announce: wait: %m");
     79  1.1      cgd 				return (FAILED);
     80  1.1      cgd 			}
     81  1.1      cgd 		} while (val != pid);
     82  1.1      cgd 		if (status&0377 > 0)	/* we were killed by some signal */
     83  1.1      cgd 			return (FAILED);
     84  1.1      cgd 		/* Get the second byte, this is the exit/return code */
     85  1.1      cgd 		return ((status >> 8) & 0377);
     86  1.1      cgd 	}
     87  1.1      cgd 	/* we are the child, go and do it */
     88  1.1      cgd 	_exit(announce_proc(request, remote_machine));
     89  1.1      cgd }
     90  1.1      cgd 
     91  1.1      cgd /*
     92  1.1      cgd  * See if the user is accepting messages. If so, announce that
     93  1.1      cgd  * a talk is requested.
     94  1.1      cgd  */
     95  1.1      cgd announce_proc(request, remote_machine)
     96  1.1      cgd 	CTL_MSG *request;
     97  1.1      cgd 	char *remote_machine;
     98  1.1      cgd {
     99  1.1      cgd 	int pid, status;
    100  1.1      cgd 	char full_tty[32];
    101  1.1      cgd 	FILE *tf;
    102  1.1      cgd 	struct stat stbuf;
    103  1.1      cgd 
    104  1.1      cgd 	(void)sprintf(full_tty, "%s/%s", _PATH_DEV, request->r_tty);
    105  1.1      cgd 	if (access(full_tty, 0) != 0)
    106  1.1      cgd 		return (FAILED);
    107  1.1      cgd 	if ((tf = fopen(full_tty, "w")) == NULL)
    108  1.1      cgd 		return (PERMISSION_DENIED);
    109  1.1      cgd 	/*
    110  1.1      cgd 	 * On first tty open, the server will have
    111  1.1      cgd 	 * it's pgrp set, so disconnect us from the
    112  1.1      cgd 	 * tty before we catch a signal.
    113  1.1      cgd 	 */
    114  1.1      cgd 	ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
    115  1.1      cgd 	if (fstat(fileno(tf), &stbuf) < 0)
    116  1.1      cgd 		return (PERMISSION_DENIED);
    117  1.1      cgd 	if ((stbuf.st_mode&020) == 0)
    118  1.1      cgd 		return (PERMISSION_DENIED);
    119  1.1      cgd 	print_mesg(tf, request, remote_machine);
    120  1.1      cgd 	fclose(tf);
    121  1.1      cgd 	return (SUCCESS);
    122  1.1      cgd }
    123  1.1      cgd 
    124  1.1      cgd #define max(a,b) ( (a) > (b) ? (a) : (b) )
    125  1.1      cgd #define N_LINES 5
    126  1.1      cgd #define N_CHARS 120
    127  1.1      cgd 
    128  1.1      cgd /*
    129  1.1      cgd  * Build a block of characters containing the message.
    130  1.1      cgd  * It is sent blank filled and in a single block to
    131  1.1      cgd  * try to keep the message in one piece if the recipient
    132  1.1      cgd  * in in vi at the time
    133  1.1      cgd  */
    134  1.1      cgd print_mesg(tf, request, remote_machine)
    135  1.1      cgd 	FILE *tf;
    136  1.1      cgd 	CTL_MSG *request;
    137  1.1      cgd 	char *remote_machine;
    138  1.1      cgd {
    139  1.1      cgd 	struct timeval clock;
    140  1.1      cgd 	struct timezone zone;
    141  1.1      cgd 	struct tm *localtime();
    142  1.1      cgd 	struct tm *localclock;
    143  1.1      cgd 	char line_buf[N_LINES][N_CHARS];
    144  1.1      cgd 	int sizes[N_LINES];
    145  1.1      cgd 	char big_buf[N_LINES*N_CHARS];
    146  1.1      cgd 	char *bptr, *lptr;
    147  1.1      cgd 	int i, j, max_size;
    148  1.1      cgd 
    149  1.1      cgd 	i = 0;
    150  1.1      cgd 	max_size = 0;
    151  1.1      cgd 	gettimeofday(&clock, &zone);
    152  1.1      cgd 	localclock = localtime( &clock.tv_sec );
    153  1.1      cgd 	(void)sprintf(line_buf[i], " ");
    154  1.1      cgd 	sizes[i] = strlen(line_buf[i]);
    155  1.1      cgd 	max_size = max(max_size, sizes[i]);
    156  1.1      cgd 	i++;
    157  1.1      cgd 	(void)sprintf(line_buf[i], "Message from Talk_Daemon@%s at %d:%02d ...",
    158  1.1      cgd 	hostname, localclock->tm_hour , localclock->tm_min );
    159  1.1      cgd 	sizes[i] = strlen(line_buf[i]);
    160  1.1      cgd 	max_size = max(max_size, sizes[i]);
    161  1.1      cgd 	i++;
    162  1.1      cgd 	(void)sprintf(line_buf[i], "talk: connection requested by %s@%s.",
    163  1.1      cgd 		request->l_name, remote_machine);
    164  1.1      cgd 	sizes[i] = strlen(line_buf[i]);
    165  1.1      cgd 	max_size = max(max_size, sizes[i]);
    166  1.1      cgd 	i++;
    167  1.1      cgd 	(void)sprintf(line_buf[i], "talk: respond with:  talk %s@%s",
    168  1.1      cgd 		request->l_name, remote_machine);
    169  1.1      cgd 	sizes[i] = strlen(line_buf[i]);
    170  1.1      cgd 	max_size = max(max_size, sizes[i]);
    171  1.1      cgd 	i++;
    172  1.1      cgd 	(void)sprintf(line_buf[i], " ");
    173  1.1      cgd 	sizes[i] = strlen(line_buf[i]);
    174  1.1      cgd 	max_size = max(max_size, sizes[i]);
    175  1.1      cgd 	i++;
    176  1.1      cgd 	bptr = big_buf;
    177  1.1      cgd 	*bptr++ = ''; /* send something to wake them up */
    178  1.1      cgd 	*bptr++ = '\r';	/* add a \r in case of raw mode */
    179  1.1      cgd 	*bptr++ = '\n';
    180  1.1      cgd 	for (i = 0; i < N_LINES; i++) {
    181  1.1      cgd 		/* copy the line into the big buffer */
    182  1.1      cgd 		lptr = line_buf[i];
    183  1.1      cgd 		while (*lptr != '\0')
    184  1.1      cgd 			*(bptr++) = *(lptr++);
    185  1.1      cgd 		/* pad out the rest of the lines with blanks */
    186  1.1      cgd 		for (j = sizes[i]; j < max_size + 2; j++)
    187  1.1      cgd 			*(bptr++) = ' ';
    188  1.1      cgd 		*(bptr++) = '\r';	/* add a \r in case of raw mode */
    189  1.1      cgd 		*(bptr++) = '\n';
    190  1.1      cgd 	}
    191  1.1      cgd 	*bptr = '\0';
    192  1.1      cgd 	fprintf(tf, big_buf);
    193  1.1      cgd 	fflush(tf);
    194  1.1      cgd 	ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
    195  1.1      cgd }
    196