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