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 #include <sys/types.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
46*7c478bd9Sstevel@tonic-gate #include <time.h>
47*7c478bd9Sstevel@tonic-gate #include <stdio.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
49*7c478bd9Sstevel@tonic-gate #include <errno.h>
50*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
51*7c478bd9Sstevel@tonic-gate #include <pwd.h>
52*7c478bd9Sstevel@tonic-gate #include <unistd.h>
53*7c478bd9Sstevel@tonic-gate #include <ctype.h>
54*7c478bd9Sstevel@tonic-gate #include <string.h>
55*7c478bd9Sstevel@tonic-gate #include "talkd_impl.h"
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static int nofork = 0;		/* to be set from the debugger */
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate static int announce_proc(CTL_MSG *request, char *remote_machine);
60*7c478bd9Sstevel@tonic-gate static void print_mesg(FILE *tf, CTL_MSG *request, char *remote_machine);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /*
63*7c478bd9Sstevel@tonic-gate  * Because the tty driver insists on attaching a terminal-less
64*7c478bd9Sstevel@tonic-gate  * process to any terminal that it writes on, we must fork a child
65*7c478bd9Sstevel@tonic-gate  * to protect ourselves.
66*7c478bd9Sstevel@tonic-gate  */
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate int
announce(CTL_MSG * request,char * remote_machine)69*7c478bd9Sstevel@tonic-gate announce(CTL_MSG *request, char *remote_machine)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	pid_t pid, val;
72*7c478bd9Sstevel@tonic-gate 	int status;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	if (nofork) {
75*7c478bd9Sstevel@tonic-gate 		return (announce_proc(request, remote_machine));
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	if (pid = fork()) {
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 		/* we are the parent, so wait for the child */
81*7c478bd9Sstevel@tonic-gate 		if (pid == (pid_t)-1) {
82*7c478bd9Sstevel@tonic-gate 			/* the fork failed */
83*7c478bd9Sstevel@tonic-gate 			return (FAILED);
84*7c478bd9Sstevel@tonic-gate 		}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 		do {
87*7c478bd9Sstevel@tonic-gate 			val = wait(&status);
88*7c478bd9Sstevel@tonic-gate 			if (val == (pid_t)-1) {
89*7c478bd9Sstevel@tonic-gate 				if (errno == EINTR) {
90*7c478bd9Sstevel@tonic-gate 					continue;
91*7c478bd9Sstevel@tonic-gate 				} else {
92*7c478bd9Sstevel@tonic-gate 					/* shouldn't happen */
93*7c478bd9Sstevel@tonic-gate 					print_error("wait");
94*7c478bd9Sstevel@tonic-gate 					return (FAILED);
95*7c478bd9Sstevel@tonic-gate 				}
96*7c478bd9Sstevel@tonic-gate 			}
97*7c478bd9Sstevel@tonic-gate 		} while (val != pid);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 		if ((status & 0377) > 0) {
100*7c478bd9Sstevel@tonic-gate 			/* we were killed by some signal */
101*7c478bd9Sstevel@tonic-gate 			return (FAILED);
102*7c478bd9Sstevel@tonic-gate 		}
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 		/* Get the second byte, this is the exit/return code */
105*7c478bd9Sstevel@tonic-gate 		return ((status>>8)&0377);
106*7c478bd9Sstevel@tonic-gate 	} else {
107*7c478bd9Sstevel@tonic-gate 		/* we are the child, go and do it */
108*7c478bd9Sstevel@tonic-gate 		_exit(announce_proc(request, remote_machine));
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate /*
115*7c478bd9Sstevel@tonic-gate  * See if the user is accepting messages. If so, announce that
116*7c478bd9Sstevel@tonic-gate  * a talk is requested.
117*7c478bd9Sstevel@tonic-gate  */
118*7c478bd9Sstevel@tonic-gate static int
announce_proc(CTL_MSG * request,char * remote_machine)119*7c478bd9Sstevel@tonic-gate announce_proc(CTL_MSG *request, char *remote_machine)
120*7c478bd9Sstevel@tonic-gate {
121*7c478bd9Sstevel@tonic-gate #define	TTY_BUFSZ	32
122*7c478bd9Sstevel@tonic-gate 	char full_tty[TTY_BUFSZ];
123*7c478bd9Sstevel@tonic-gate 	FILE *tf;
124*7c478bd9Sstevel@tonic-gate 	struct stat stbuf;
125*7c478bd9Sstevel@tonic-gate 	int fd;
126*7c478bd9Sstevel@tonic-gate 	struct passwd *p;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	(void) snprintf(full_tty, TTY_BUFSZ, "/dev/%s", request->r_tty);
129*7c478bd9Sstevel@tonic-gate 	p = getpwnam(request->r_name);
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	if (p == 0 || access(full_tty, 0) != 0) {
132*7c478bd9Sstevel@tonic-gate 		return (FAILED);
133*7c478bd9Sstevel@tonic-gate 	}
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	/* fopen uses O_CREAT|O_TRUNC, we don't want that */
136*7c478bd9Sstevel@tonic-gate 	if ((fd = open(full_tty, O_WRONLY|O_NONBLOCK)) == -1) {
137*7c478bd9Sstevel@tonic-gate 		return (PERMISSION_DENIED);
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 	/* must be tty */
140*7c478bd9Sstevel@tonic-gate 	if (!isatty(fd)) {
141*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
142*7c478bd9Sstevel@tonic-gate 		return (PERMISSION_DENIED);
143*7c478bd9Sstevel@tonic-gate 	}
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	/*
146*7c478bd9Sstevel@tonic-gate 	 * open gratuitously attaches the talkd to any tty it opens, so
147*7c478bd9Sstevel@tonic-gate 	 * disconnect us from the tty before we catch a signal
148*7c478bd9Sstevel@tonic-gate 	 */
149*7c478bd9Sstevel@tonic-gate 	(void) setsid();
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	if (fstat(fd, &stbuf) < 0 || stbuf.st_uid != p->pw_uid) {
152*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
153*7c478bd9Sstevel@tonic-gate 		return (PERMISSION_DENIED);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mode&020) == 0) {
157*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
158*7c478bd9Sstevel@tonic-gate 		return (PERMISSION_DENIED);
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	if ((tf = fdopen(fd, "w")) == NULL) {
162*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
163*7c478bd9Sstevel@tonic-gate 		return (PERMISSION_DENIED);
164*7c478bd9Sstevel@tonic-gate 	}
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	print_mesg(tf, request, remote_machine);
167*7c478bd9Sstevel@tonic-gate 	(void) fclose(tf);
168*7c478bd9Sstevel@tonic-gate 	return (SUCCESS);
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate #define	max(a, b) ((a) > (b) ? (a) : (b))
172*7c478bd9Sstevel@tonic-gate #define	N_LINES	5
173*7c478bd9Sstevel@tonic-gate #define	N_CHARS	300
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate /*
176*7c478bd9Sstevel@tonic-gate  * Build a block of characters containing the message.
177*7c478bd9Sstevel@tonic-gate  * It is sent blank filled and in a single block to
178*7c478bd9Sstevel@tonic-gate  * try to keep the message in one piece if the recipient
179*7c478bd9Sstevel@tonic-gate  * is in vi at the time.
180*7c478bd9Sstevel@tonic-gate  */
181*7c478bd9Sstevel@tonic-gate static void
print_mesg(FILE * tf,CTL_MSG * request,char * remote_machine)182*7c478bd9Sstevel@tonic-gate print_mesg(FILE *tf, CTL_MSG *request, char *remote_machine)
183*7c478bd9Sstevel@tonic-gate {
184*7c478bd9Sstevel@tonic-gate 	struct timeval clock;
185*7c478bd9Sstevel@tonic-gate 	struct tm *localclock;
186*7c478bd9Sstevel@tonic-gate 	char line_buf[N_LINES][N_CHARS];
187*7c478bd9Sstevel@tonic-gate 	int sizes[N_LINES];
188*7c478bd9Sstevel@tonic-gate 	char *bptr, *lptr;
189*7c478bd9Sstevel@tonic-gate 	int i, j, max_size;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	/*
192*7c478bd9Sstevel@tonic-gate 	 * [3 wakeup chars + (lines * max chars/line) +
193*7c478bd9Sstevel@tonic-gate 	 * (lines * strlen("\r\n")) + 1(NUL)].
194*7c478bd9Sstevel@tonic-gate 	 */
195*7c478bd9Sstevel@tonic-gate 	char big_buf[3 + (N_LINES * (N_CHARS - 1)) + (N_LINES * 2) + 1];
196*7c478bd9Sstevel@tonic-gate 	/*
197*7c478bd9Sstevel@tonic-gate 	 * ( (length of (request->l_name) - 1(NUL)) *
198*7c478bd9Sstevel@tonic-gate 	 * (strlen("M-") + 1('^') + 1(printable char)) ) + 1(NUL).
199*7c478bd9Sstevel@tonic-gate 	 */
200*7c478bd9Sstevel@tonic-gate 	char l_username[((NAME_SIZE - 1) * 4) + 1];
201*7c478bd9Sstevel@tonic-gate 	int len, k;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	i = 0;
204*7c478bd9Sstevel@tonic-gate 	max_size = 0;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&clock, NULL);
207*7c478bd9Sstevel@tonic-gate 	localclock = localtime(&clock.tv_sec);
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	(void) sprintf(line_buf[i], " ");
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	sizes[i] = strlen(line_buf[i]);
212*7c478bd9Sstevel@tonic-gate 	max_size = max(max_size, sizes[i]);
213*7c478bd9Sstevel@tonic-gate 	i++;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	(void) snprintf(line_buf[i], N_CHARS,
216*7c478bd9Sstevel@tonic-gate 	    "Message from Talk_Daemon@%s at %d:%02d ...", hostname,
217*7c478bd9Sstevel@tonic-gate 	    localclock->tm_hour, localclock->tm_min);
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	sizes[i] = strlen(line_buf[i]);
220*7c478bd9Sstevel@tonic-gate 	max_size = max(max_size, sizes[i]);
221*7c478bd9Sstevel@tonic-gate 	i++;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	len = (strlen(request->l_name) > NAME_SIZE - 1) ? (NAME_SIZE - 1) :
224*7c478bd9Sstevel@tonic-gate 	    strlen(request->l_name);
225*7c478bd9Sstevel@tonic-gate 	for (j = 0, k = 0; j < len; j++) {
226*7c478bd9Sstevel@tonic-gate 		if (!isprint((unsigned char)request->l_name[j])) {
227*7c478bd9Sstevel@tonic-gate 			char c;
228*7c478bd9Sstevel@tonic-gate 			if (!isascii((unsigned char)request->l_name[j])) {
229*7c478bd9Sstevel@tonic-gate 				l_username[k++] = 'M';
230*7c478bd9Sstevel@tonic-gate 				l_username[k++] = '-';
231*7c478bd9Sstevel@tonic-gate 				c = toascii(request->l_name[j]);
232*7c478bd9Sstevel@tonic-gate 			}
233*7c478bd9Sstevel@tonic-gate 			if (iscntrl((unsigned char)request->l_name[j])) {
234*7c478bd9Sstevel@tonic-gate 				l_username[k++] = '^';
235*7c478bd9Sstevel@tonic-gate 				/* add decimal 64 to the control character */
236*7c478bd9Sstevel@tonic-gate 				c = request->l_name[j] + 0100;
237*7c478bd9Sstevel@tonic-gate 			}
238*7c478bd9Sstevel@tonic-gate 			l_username[k++] = c;
239*7c478bd9Sstevel@tonic-gate 		} else {
240*7c478bd9Sstevel@tonic-gate 			l_username[k++] = request->l_name[j];
241*7c478bd9Sstevel@tonic-gate 		}
242*7c478bd9Sstevel@tonic-gate 	}
243*7c478bd9Sstevel@tonic-gate 	l_username[k] = '\0';
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	(void) snprintf(line_buf[i], N_CHARS,
246*7c478bd9Sstevel@tonic-gate 	    "talk: connection requested by %s@%s.", l_username, remote_machine);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	sizes[i] = strlen(line_buf[i]);
249*7c478bd9Sstevel@tonic-gate 	max_size = max(max_size, sizes[i]);
250*7c478bd9Sstevel@tonic-gate 	i++;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	(void) snprintf(line_buf[i], N_CHARS, "talk: respond with:  talk %s@%s",
253*7c478bd9Sstevel@tonic-gate 	    l_username, remote_machine);
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	sizes[i] = strlen(line_buf[i]);
256*7c478bd9Sstevel@tonic-gate 	max_size = max(max_size, sizes[i]);
257*7c478bd9Sstevel@tonic-gate 	i++;
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	(void) sprintf(line_buf[i], " ");
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 	sizes[i] = strlen(line_buf[i]);
262*7c478bd9Sstevel@tonic-gate 	max_size = max(max_size, sizes[i]);
263*7c478bd9Sstevel@tonic-gate 	i++;
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	bptr = big_buf;
266*7c478bd9Sstevel@tonic-gate 	*(bptr++) = '\a';	/* send something to wake them up */
267*7c478bd9Sstevel@tonic-gate 	*(bptr++) = '\r';	/* add a \r in case of raw mode */
268*7c478bd9Sstevel@tonic-gate 	*(bptr++) = '\n';
269*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < N_LINES; i++) {
270*7c478bd9Sstevel@tonic-gate 		/* copy the line into the big buffer */
271*7c478bd9Sstevel@tonic-gate 		lptr = line_buf[i];
272*7c478bd9Sstevel@tonic-gate 		while (*lptr != '\0') {
273*7c478bd9Sstevel@tonic-gate 			*(bptr++) = *(lptr++);
274*7c478bd9Sstevel@tonic-gate 		}
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 		/* pad out the rest of the lines with blanks */
277*7c478bd9Sstevel@tonic-gate 		for (j = sizes[i]; j < max_size; j++) {
278*7c478bd9Sstevel@tonic-gate 			*(bptr++) = ' ';
279*7c478bd9Sstevel@tonic-gate 		}
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 		*(bptr++) = '\r';	/* add a \r in case of raw mode */
282*7c478bd9Sstevel@tonic-gate 		*(bptr++) = '\n';
283*7c478bd9Sstevel@tonic-gate 	}
284*7c478bd9Sstevel@tonic-gate 	*bptr = '\0';
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 	(void) fputs(big_buf, tf);
287*7c478bd9Sstevel@tonic-gate 	(void) fflush(tf);
288*7c478bd9Sstevel@tonic-gate 	(void) setsid();
289*7c478bd9Sstevel@tonic-gate }
290