Home | History | Annotate | Line # | Download | only in talkd
announce.c revision 1.3
      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.3 1994/12/21 20:03:48 glass 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 <string.h>
     51 #include <vis.h>
     52 #include <paths.h>
     53 
     54 extern char hostname[];
     55 
     56 /*
     57  * Announce an invitation to talk.
     58  *
     59  * Because the tty driver insists on attaching a terminal-less
     60  * process to any terminal that it writes on, we must fork a child
     61  * to protect ourselves
     62  */
     63 announce(request, remote_machine)
     64 	CTL_MSG *request;
     65 	char *remote_machine;
     66 {
     67 	int pid, val, status;
     68 
     69 	if (pid = fork()) {
     70 		/* we are the parent, so wait for the child */
     71 		if (pid == -1)		/* the fork failed */
     72 			return (FAILED);
     73 		do {
     74 			val = wait(&status);
     75 			if (val == -1) {
     76 				if (errno == EINTR)
     77 					continue;
     78 				/* shouldn't happen */
     79 				syslog(LOG_WARNING, "announce: wait: %m");
     80 				return (FAILED);
     81 			}
     82 		} while (val != pid);
     83 		if (status&0377 > 0)	/* we were killed by some signal */
     84 			return (FAILED);
     85 		/* Get the second byte, this is the exit/return code */
     86 		return ((status >> 8) & 0377);
     87 	}
     88 	/* we are the child, go and do it */
     89 	_exit(announce_proc(request, remote_machine));
     90 }
     91 
     92 /*
     93  * See if the user is accepting messages. If so, announce that
     94  * a talk is requested.
     95  */
     96 announce_proc(request, remote_machine)
     97 	CTL_MSG *request;
     98 	char *remote_machine;
     99 {
    100 	int pid, status;
    101 	char full_tty[32];
    102 	FILE *tf;
    103 	struct stat stbuf;
    104 
    105 	(void)sprintf(full_tty, "%s/%s", _PATH_DEV, request->r_tty);
    106 	if (access(full_tty, 0) != 0)
    107 		return (FAILED);
    108 	if ((tf = fopen(full_tty, "w")) == NULL)
    109 		return (PERMISSION_DENIED);
    110 	/*
    111 	 * On first tty open, the server will have
    112 	 * it's pgrp set, so disconnect us from the
    113 	 * tty before we catch a signal.
    114 	 */
    115 	ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
    116 	if (fstat(fileno(tf), &stbuf) < 0)
    117 		return (PERMISSION_DENIED);
    118 	if ((stbuf.st_mode&020) == 0)
    119 		return (PERMISSION_DENIED);
    120 	print_mesg(tf, request, remote_machine);
    121 	fclose(tf);
    122 	return (SUCCESS);
    123 }
    124 
    125 #define max(a,b) ( (a) > (b) ? (a) : (b) )
    126 #define N_LINES 5
    127 #define N_CHARS 120
    128 
    129 /*
    130  * Build a block of characters containing the message.
    131  * It is sent blank filled and in a single block to
    132  * try to keep the message in one piece if the recipient
    133  * in in vi at the time
    134  */
    135 print_mesg(tf, request, remote_machine)
    136 	FILE *tf;
    137 	CTL_MSG *request;
    138 	char *remote_machine;
    139 {
    140 	struct timeval clock;
    141 	struct timezone zone;
    142 	struct tm *localtime();
    143 	struct tm *localclock;
    144 	char line_buf[N_LINES][N_CHARS];
    145 	int sizes[N_LINES];
    146 	char big_buf[N_LINES*N_CHARS];
    147 	char *bptr, *lptr, *vis_user;
    148 	int i, j, max_size;
    149 
    150 	i = 0;
    151 	max_size = 0;
    152 	gettimeofday(&clock, &zone);
    153 	localclock = localtime( &clock.tv_sec );
    154 	(void)sprintf(line_buf[i], " ");
    155 	sizes[i] = strlen(line_buf[i]);
    156 	max_size = max(max_size, sizes[i]);
    157 	i++;
    158 	(void)sprintf(line_buf[i], "Message from Talk_Daemon@%s at %d:%02d ...",
    159 	hostname, localclock->tm_hour , localclock->tm_min );
    160 	sizes[i] = strlen(line_buf[i]);
    161 	max_size = max(max_size, sizes[i]);
    162 	i++;
    163 	vis_user = (char *) malloc(strlen(request->l_name) * 4 + 1);
    164 	strvis(vis_user, request->l_name, VIS_CSTYLE);
    165 	(void)sprintf(line_buf[i], "talk: connection requested by %s@%s.",
    166 		vis_user, remote_machine);
    167 	sizes[i] = strlen(line_buf[i]);
    168 	max_size = max(max_size, sizes[i]);
    169 	i++;
    170 	(void)sprintf(line_buf[i], "talk: respond with:  talk %s@%s",
    171 		vis_user, remote_machine);
    172 	sizes[i] = strlen(line_buf[i]);
    173 	max_size = max(max_size, sizes[i]);
    174 	i++;
    175 	(void)sprintf(line_buf[i], " ");
    176 	sizes[i] = strlen(line_buf[i]);
    177 	max_size = max(max_size, sizes[i]);
    178 	i++;
    179 	bptr = big_buf;
    180 	*bptr++ = ''; /* send something to wake them up */
    181 	*bptr++ = '\r';	/* add a \r in case of raw mode */
    182 	*bptr++ = '\n';
    183 	for (i = 0; i < N_LINES; i++) {
    184 		/* copy the line into the big buffer */
    185 		lptr = line_buf[i];
    186 		while (*lptr != '\0')
    187 			*(bptr++) = *(lptr++);
    188 		/* pad out the rest of the lines with blanks */
    189 		for (j = sizes[i]; j < max_size + 2; j++)
    190 			*(bptr++) = ' ';
    191 		*(bptr++) = '\r';	/* add a \r in case of raw mode */
    192 		*(bptr++) = '\n';
    193 	}
    194 	*bptr = '\0';
    195 	fprintf(tf, big_buf);
    196 	fflush(tf);
    197 	ioctl(fileno(tf), TIOCNOTTY, (struct sgttyb *) 0);
    198 }
    199