xref: /titanic_50/usr/src/cmd/cmd-inet/usr.sbin/in.talkd/announce.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
23*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
28*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
33*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California.
34*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
37*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
38*7c478bd9Sstevel@tonic-gate  * contributors.
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
49*7c478bd9Sstevel@tonic-gate #include <time.h>
50*7c478bd9Sstevel@tonic-gate #include <stdio.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
52*7c478bd9Sstevel@tonic-gate #include <errno.h>
53*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
54*7c478bd9Sstevel@tonic-gate #include <pwd.h>
55*7c478bd9Sstevel@tonic-gate #include <unistd.h>
56*7c478bd9Sstevel@tonic-gate #include <ctype.h>
57*7c478bd9Sstevel@tonic-gate #include <string.h>
58*7c478bd9Sstevel@tonic-gate #include "talkd_impl.h"
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate static int nofork = 0;		/* to be set from the debugger */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate static int announce_proc(CTL_MSG *request, char *remote_machine);
63*7c478bd9Sstevel@tonic-gate static void print_mesg(FILE *tf, CTL_MSG *request, char *remote_machine);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate /*
66*7c478bd9Sstevel@tonic-gate  * Because the tty driver insists on attaching a terminal-less
67*7c478bd9Sstevel@tonic-gate  * process to any terminal that it writes on, we must fork a child
68*7c478bd9Sstevel@tonic-gate  * to protect ourselves.
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate int
announce(CTL_MSG * request,char * remote_machine)72*7c478bd9Sstevel@tonic-gate announce(CTL_MSG *request, char *remote_machine)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	pid_t pid, val;
75*7c478bd9Sstevel@tonic-gate 	int status;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if (nofork) {
78*7c478bd9Sstevel@tonic-gate 		return (announce_proc(request, remote_machine));
79*7c478bd9Sstevel@tonic-gate 	}
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if (pid = fork()) {
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 		/* we are the parent, so wait for the child */
84*7c478bd9Sstevel@tonic-gate 		if (pid == (pid_t)-1) {
85*7c478bd9Sstevel@tonic-gate 			/* the fork failed */
86*7c478bd9Sstevel@tonic-gate 			return (FAILED);
87*7c478bd9Sstevel@tonic-gate 		}
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 		do {
90*7c478bd9Sstevel@tonic-gate 			val = wait(&status);
91*7c478bd9Sstevel@tonic-gate 			if (val == (pid_t)-1) {
92*7c478bd9Sstevel@tonic-gate 				if (errno == EINTR) {
93*7c478bd9Sstevel@tonic-gate 					continue;
94*7c478bd9Sstevel@tonic-gate 				} else {
95*7c478bd9Sstevel@tonic-gate 					/* shouldn't happen */
96*7c478bd9Sstevel@tonic-gate 					print_error("wait");
97*7c478bd9Sstevel@tonic-gate 					return (FAILED);
98*7c478bd9Sstevel@tonic-gate 				}
99*7c478bd9Sstevel@tonic-gate 			}
100*7c478bd9Sstevel@tonic-gate 		} while (val != pid);
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 		if ((status & 0377) > 0) {
103*7c478bd9Sstevel@tonic-gate 			/* we were killed by some signal */
104*7c478bd9Sstevel@tonic-gate 			return (FAILED);
105*7c478bd9Sstevel@tonic-gate 		}
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 		/* Get the second byte, this is the exit/return code */
108*7c478bd9Sstevel@tonic-gate 		return ((status>>8)&0377);
109*7c478bd9Sstevel@tonic-gate 	} else {
110*7c478bd9Sstevel@tonic-gate 		/* we are the child, go and do it */
111*7c478bd9Sstevel@tonic-gate 		_exit(announce_proc(request, remote_machine));
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate /*
118*7c478bd9Sstevel@tonic-gate  * See if the user is accepting messages. If so, announce that
119*7c478bd9Sstevel@tonic-gate  * a talk is requested.
120*7c478bd9Sstevel@tonic-gate  */
121*7c478bd9Sstevel@tonic-gate static int
announce_proc(CTL_MSG * request,char * remote_machine)122*7c478bd9Sstevel@tonic-gate announce_proc(CTL_MSG *request, char *remote_machine)
123*7c478bd9Sstevel@tonic-gate {
124*7c478bd9Sstevel@tonic-gate #define	TTY_BUFSZ	32
125*7c478bd9Sstevel@tonic-gate 	char full_tty[TTY_BUFSZ];
126*7c478bd9Sstevel@tonic-gate 	FILE *tf;
127*7c478bd9Sstevel@tonic-gate 	struct stat stbuf;
128*7c478bd9Sstevel@tonic-gate 	int fd;
129*7c478bd9Sstevel@tonic-gate 	struct passwd *p;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	(void) snprintf(full_tty, TTY_BUFSZ, "/dev/%s", request->r_tty);
132*7c478bd9Sstevel@tonic-gate 	p = getpwnam(request->r_name);
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	if (p == 0 || access(full_tty, 0) != 0) {
135*7c478bd9Sstevel@tonic-gate 		return (FAILED);
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	/* fopen uses O_CREAT|O_TRUNC, we don't want that */
139*7c478bd9Sstevel@tonic-gate 	if ((fd = open(full_tty, O_WRONLY|O_NONBLOCK)) == -1) {
140*7c478bd9Sstevel@tonic-gate 		return (PERMISSION_DENIED);
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 	/* must be tty */
143*7c478bd9Sstevel@tonic-gate 	if (!isatty(fd)) {
144*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
145*7c478bd9Sstevel@tonic-gate 		return (PERMISSION_DENIED);
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	/*
149*7c478bd9Sstevel@tonic-gate 	 * open gratuitously attaches the talkd to any tty it opens, so
150*7c478bd9Sstevel@tonic-gate 	 * disconnect us from the tty before we catch a signal
151*7c478bd9Sstevel@tonic-gate 	 */
152*7c478bd9Sstevel@tonic-gate 	(void) setsid();
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	if (fstat(fd, &stbuf) < 0 || stbuf.st_uid != p->pw_uid) {
155*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
156*7c478bd9Sstevel@tonic-gate 		return (PERMISSION_DENIED);
157*7c478bd9Sstevel@tonic-gate 	}
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mode&020) == 0) {
160*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
161*7c478bd9Sstevel@tonic-gate 		return (PERMISSION_DENIED);
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	if ((tf = fdopen(fd, "w")) == NULL) {
165*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
166*7c478bd9Sstevel@tonic-gate 		return (PERMISSION_DENIED);
167*7c478bd9Sstevel@tonic-gate 	}
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	print_mesg(tf, request, remote_machine);
170*7c478bd9Sstevel@tonic-gate 	(void) fclose(tf);
171*7c478bd9Sstevel@tonic-gate 	return (SUCCESS);
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate #define	max(a, b) ((a) > (b) ? (a) : (b))
175*7c478bd9Sstevel@tonic-gate #define	N_LINES	5
176*7c478bd9Sstevel@tonic-gate #define	N_CHARS	300
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate /*
179*7c478bd9Sstevel@tonic-gate  * Build a block of characters containing the message.
180*7c478bd9Sstevel@tonic-gate  * It is sent blank filled and in a single block to
181*7c478bd9Sstevel@tonic-gate  * try to keep the message in one piece if the recipient
182*7c478bd9Sstevel@tonic-gate  * is in vi at the time.
183*7c478bd9Sstevel@tonic-gate  */
184*7c478bd9Sstevel@tonic-gate static void
print_mesg(FILE * tf,CTL_MSG * request,char * remote_machine)185*7c478bd9Sstevel@tonic-gate print_mesg(FILE *tf, CTL_MSG *request, char *remote_machine)
186*7c478bd9Sstevel@tonic-gate {
187*7c478bd9Sstevel@tonic-gate 	struct timeval clock;
188*7c478bd9Sstevel@tonic-gate 	struct tm *localclock;
189*7c478bd9Sstevel@tonic-gate 	char line_buf[N_LINES][N_CHARS];
190*7c478bd9Sstevel@tonic-gate 	int sizes[N_LINES];
191*7c478bd9Sstevel@tonic-gate 	char *bptr, *lptr;
192*7c478bd9Sstevel@tonic-gate 	int i, j, max_size;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	/*
195*7c478bd9Sstevel@tonic-gate 	 * [3 wakeup chars + (lines * max chars/line) +
196*7c478bd9Sstevel@tonic-gate 	 * (lines * strlen("\r\n")) + 1(NUL)].
197*7c478bd9Sstevel@tonic-gate 	 */
198*7c478bd9Sstevel@tonic-gate 	char big_buf[3 + (N_LINES * (N_CHARS - 1)) + (N_LINES * 2) + 1];
199*7c478bd9Sstevel@tonic-gate 	/*
200*7c478bd9Sstevel@tonic-gate 	 * ( (length of (request->l_name) - 1(NUL)) *
201*7c478bd9Sstevel@tonic-gate 	 * (strlen("M-") + 1('^') + 1(printable char)) ) + 1(NUL).
202*7c478bd9Sstevel@tonic-gate 	 */
203*7c478bd9Sstevel@tonic-gate 	char l_username[((NAME_SIZE - 1) * 4) + 1];
204*7c478bd9Sstevel@tonic-gate 	int len, k;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	i = 0;
207*7c478bd9Sstevel@tonic-gate 	max_size = 0;
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&clock, NULL);
210*7c478bd9Sstevel@tonic-gate 	localclock = localtime(&clock.tv_sec);
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	(void) sprintf(line_buf[i], " ");
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	sizes[i] = strlen(line_buf[i]);
215*7c478bd9Sstevel@tonic-gate 	max_size = max(max_size, sizes[i]);
216*7c478bd9Sstevel@tonic-gate 	i++;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	(void) snprintf(line_buf[i], N_CHARS,
219*7c478bd9Sstevel@tonic-gate 	    "Message from Talk_Daemon@%s at %d:%02d ...", hostname,
220*7c478bd9Sstevel@tonic-gate 	    localclock->tm_hour, localclock->tm_min);
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	sizes[i] = strlen(line_buf[i]);
223*7c478bd9Sstevel@tonic-gate 	max_size = max(max_size, sizes[i]);
224*7c478bd9Sstevel@tonic-gate 	i++;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	len = (strlen(request->l_name) > NAME_SIZE - 1) ? (NAME_SIZE - 1) :
227*7c478bd9Sstevel@tonic-gate 	    strlen(request->l_name);
228*7c478bd9Sstevel@tonic-gate 	for (j = 0, k = 0; j < len; j++) {
229*7c478bd9Sstevel@tonic-gate 		if (!isprint((unsigned char)request->l_name[j])) {
230*7c478bd9Sstevel@tonic-gate 			char c;
231*7c478bd9Sstevel@tonic-gate 			if (!isascii((unsigned char)request->l_name[j])) {
232*7c478bd9Sstevel@tonic-gate 				l_username[k++] = 'M';
233*7c478bd9Sstevel@tonic-gate 				l_username[k++] = '-';
234*7c478bd9Sstevel@tonic-gate 				c = toascii(request->l_name[j]);
235*7c478bd9Sstevel@tonic-gate 			}
236*7c478bd9Sstevel@tonic-gate 			if (iscntrl((unsigned char)request->l_name[j])) {
237*7c478bd9Sstevel@tonic-gate 				l_username[k++] = '^';
238*7c478bd9Sstevel@tonic-gate 				/* add decimal 64 to the control character */
239*7c478bd9Sstevel@tonic-gate 				c = request->l_name[j] + 0100;
240*7c478bd9Sstevel@tonic-gate 			}
241*7c478bd9Sstevel@tonic-gate 			l_username[k++] = c;
242*7c478bd9Sstevel@tonic-gate 		} else {
243*7c478bd9Sstevel@tonic-gate 			l_username[k++] = request->l_name[j];
244*7c478bd9Sstevel@tonic-gate 		}
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 	l_username[k] = '\0';
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	(void) snprintf(line_buf[i], N_CHARS,
249*7c478bd9Sstevel@tonic-gate 	    "talk: connection requested by %s@%s.", l_username, remote_machine);
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	sizes[i] = strlen(line_buf[i]);
252*7c478bd9Sstevel@tonic-gate 	max_size = max(max_size, sizes[i]);
253*7c478bd9Sstevel@tonic-gate 	i++;
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	(void) snprintf(line_buf[i], N_CHARS, "talk: respond with:  talk %s@%s",
256*7c478bd9Sstevel@tonic-gate 	    l_username, remote_machine);
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	sizes[i] = strlen(line_buf[i]);
259*7c478bd9Sstevel@tonic-gate 	max_size = max(max_size, sizes[i]);
260*7c478bd9Sstevel@tonic-gate 	i++;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	(void) sprintf(line_buf[i], " ");
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	sizes[i] = strlen(line_buf[i]);
265*7c478bd9Sstevel@tonic-gate 	max_size = max(max_size, sizes[i]);
266*7c478bd9Sstevel@tonic-gate 	i++;
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 	bptr = big_buf;
269*7c478bd9Sstevel@tonic-gate 	*(bptr++) = '\a';	/* send something to wake them up */
270*7c478bd9Sstevel@tonic-gate 	*(bptr++) = '\r';	/* add a \r in case of raw mode */
271*7c478bd9Sstevel@tonic-gate 	*(bptr++) = '\n';
272*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < N_LINES; i++) {
273*7c478bd9Sstevel@tonic-gate 		/* copy the line into the big buffer */
274*7c478bd9Sstevel@tonic-gate 		lptr = line_buf[i];
275*7c478bd9Sstevel@tonic-gate 		while (*lptr != '\0') {
276*7c478bd9Sstevel@tonic-gate 			*(bptr++) = *(lptr++);
277*7c478bd9Sstevel@tonic-gate 		}
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 		/* pad out the rest of the lines with blanks */
280*7c478bd9Sstevel@tonic-gate 		for (j = sizes[i]; j < max_size; j++) {
281*7c478bd9Sstevel@tonic-gate 			*(bptr++) = ' ';
282*7c478bd9Sstevel@tonic-gate 		}
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 		*(bptr++) = '\r';	/* add a \r in case of raw mode */
285*7c478bd9Sstevel@tonic-gate 		*(bptr++) = '\n';
286*7c478bd9Sstevel@tonic-gate 	}
287*7c478bd9Sstevel@tonic-gate 	*bptr = '\0';
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	(void) fputs(big_buf, tf);
290*7c478bd9Sstevel@tonic-gate 	(void) fflush(tf);
291*7c478bd9Sstevel@tonic-gate 	(void) setsid();
292*7c478bd9Sstevel@tonic-gate }
293