17c478bd9Sstevel@tonic-gate /*
2*faebf794Sgtb  * CDDL HEADER START
3*faebf794Sgtb  *
4*faebf794Sgtb  * The contents of this file are subject to the terms of the
5*faebf794Sgtb  * Common Development and Distribution License (the "License").
6*faebf794Sgtb  * You may not use this file except in compliance with the License.
7*faebf794Sgtb  *
8*faebf794Sgtb  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*faebf794Sgtb  * or http://www.opensolaris.org/os/licensing.
10*faebf794Sgtb  * See the License for the specific language governing permissions
11*faebf794Sgtb  * and limitations under the License.
12*faebf794Sgtb  *
13*faebf794Sgtb  * When distributing Covered Code, include this CDDL HEADER in each
14*faebf794Sgtb  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*faebf794Sgtb  * If applicable, add the following below this CDDL HEADER, with the
16*faebf794Sgtb  * fields enclosed by brackets "[]" replaced with your own identifying
17*faebf794Sgtb  * information: Portions Copyright [yyyy] [name of copyright owner]
18*faebf794Sgtb  *
19*faebf794Sgtb  * CDDL HEADER END
20*faebf794Sgtb  */
21*faebf794Sgtb /*
22*faebf794Sgtb  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include	<ctype.h>
277c478bd9Sstevel@tonic-gate #include	<string.h>
287c478bd9Sstevel@tonic-gate #include	<stdio.h>
297c478bd9Sstevel@tonic-gate #include	<signal.h>
307c478bd9Sstevel@tonic-gate #include	<sys/wait.h>
317c478bd9Sstevel@tonic-gate #include	<sys/types.h>
327c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
337c478bd9Sstevel@tonic-gate #include	<stdlib.h>
347c478bd9Sstevel@tonic-gate #include	<unistd.h>
357c478bd9Sstevel@tonic-gate #include	<time.h>
367c478bd9Sstevel@tonic-gate #include	<utmpx.h>
377c478bd9Sstevel@tonic-gate #include	<pwd.h>
387c478bd9Sstevel@tonic-gate #include	<fcntl.h>
397c478bd9Sstevel@tonic-gate #include	<stdarg.h>
407c478bd9Sstevel@tonic-gate #include	<locale.h>
417c478bd9Sstevel@tonic-gate #include	<stdlib.h>
427c478bd9Sstevel@tonic-gate #include	<limits.h>
437c478bd9Sstevel@tonic-gate #include	<wctype.h>
447c478bd9Sstevel@tonic-gate #include	<errno.h>
457c478bd9Sstevel@tonic-gate #include	<syslog.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define		TRUE	1
487c478bd9Sstevel@tonic-gate #define		FALSE	0
497c478bd9Sstevel@tonic-gate #define		FAILURE	-1
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  *	DATE-TIME format
527c478bd9Sstevel@tonic-gate  *  %a	abbreviated weekday name
537c478bd9Sstevel@tonic-gate  *  %b  abbreviated month name
547c478bd9Sstevel@tonic-gate  *  %e  day of month
557c478bd9Sstevel@tonic-gate  *  %H  hour - 24 hour clock
567c478bd9Sstevel@tonic-gate  *  %M  minute
577c478bd9Sstevel@tonic-gate  *  %S  second
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
61*faebf794Sgtb extern char myhostname[];
62*faebf794Sgtb extern char progname[];
63*faebf794Sgtb 
64*faebf794Sgtb 
657c478bd9Sstevel@tonic-gate static void openfail(int);
667c478bd9Sstevel@tonic-gate static void eof(void);
677c478bd9Sstevel@tonic-gate static void setsignals(void (*)());
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static FILE	*fp;	/* File pointer for receipient's terminal */
707c478bd9Sstevel@tonic-gate static char *rterm; /* Pointer to receipient's terminal */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate int
warn_send(char * receipient,char * msg)737c478bd9Sstevel@tonic-gate warn_send(char *receipient, char *msg)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	register struct utmpx *ubuf;
767c478bd9Sstevel@tonic-gate 	static char rterminal[] = "/dev/\0 2345678901";
777c478bd9Sstevel@tonic-gate 	extern FILE *fp;
787c478bd9Sstevel@tonic-gate 	time_t tod;
797c478bd9Sstevel@tonic-gate 	char time_buf[40];
807c478bd9Sstevel@tonic-gate 	register int bad = 0;
817c478bd9Sstevel@tonic-gate 	char	*rcp1, *rcp2, *rcp3;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
847c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
857c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*	Set "rterm" to location where receipient's terminal will go.	*/
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	rterm = &rterminal[sizeof ("/dev/") - 1];
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * strip any realm or instance from principal so we can match against unix
967c478bd9Sstevel@tonic-gate  * userid.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate 	rcp1 = strdup(receipient);
997c478bd9Sstevel@tonic-gate 	rcp2 = strtok(rcp1, "@");
1007c478bd9Sstevel@tonic-gate 	rcp3 = strtok(rcp2, "/");
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  *	Scan through the "utmpx" file for the
1047c478bd9Sstevel@tonic-gate  *	entry for the person we want to send to.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	setutxent();
1087c478bd9Sstevel@tonic-gate 	while ((ubuf = getutxent()) != NULL) {
1097c478bd9Sstevel@tonic-gate 		if (ubuf->ut_type == USER_PROCESS) {
1107c478bd9Sstevel@tonic-gate 			if (strncmp(rcp3, ubuf->ut_user,
1117c478bd9Sstevel@tonic-gate 				sizeof (ubuf->ut_user)) == 0) {
1127c478bd9Sstevel@tonic-gate 				strncpy(rterm, &ubuf->ut_line[0],
1137c478bd9Sstevel@tonic-gate 					sizeof (ubuf->ut_line)+1);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*	Try to open up the line to the receipient's terminal.		*/
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 				signal(SIGALRM, openfail);
1187c478bd9Sstevel@tonic-gate 				alarm(5);
1197c478bd9Sstevel@tonic-gate 				fp = fopen(&rterminal[0], "w");
1207c478bd9Sstevel@tonic-gate 				alarm(0);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*	Catch signals SIGHUP, SIGINT, SIGQUIT, and SIGTERM, and send	*/
1237c478bd9Sstevel@tonic-gate /*	<EOT> message to receipient.			*/
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 				setsignals(eof);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*	Get the time of day, convert it to a string and throw away the	*/
1287c478bd9Sstevel@tonic-gate /*	year information at the end of the string.			*/
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 				time(&tod);
1317c478bd9Sstevel@tonic-gate 				cftime(time_buf, "%c", &tod);
1327c478bd9Sstevel@tonic-gate 				(void) fprintf(fp, gettext(
133*faebf794Sgtb 	    "\r\n\007\007\007\tMessage from %s@%s [ %s ] ...\r\n"),
134*faebf794Sgtb 					    progname, myhostname, time_buf);
1357c478bd9Sstevel@tonic-gate 				sleep(1);
136*faebf794Sgtb 				fprintf(fp, gettext("\r\nMessage to %s"), msg);
1377c478bd9Sstevel@tonic-gate 				fflush(fp);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*	Since "end of file" received, send <EOT> message to receipient.	*/
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 				eof();
1427c478bd9Sstevel@tonic-gate 				fclose(fp);
1437c478bd9Sstevel@tonic-gate 			}
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	free(rcp1);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*	Did we find a place to talk to?  If we were looking for a */
1507c478bd9Sstevel@tonic-gate /*	specific spot and didn't find it, complain and log it. */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (*rterm == '\0')
1537c478bd9Sstevel@tonic-gate 		if (bad > 0) {
1547c478bd9Sstevel@tonic-gate 			(void) syslog(LOG_ERR, gettext("no place to send.\n"));
1557c478bd9Sstevel@tonic-gate 			return (1);
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	endutxent();
1597c478bd9Sstevel@tonic-gate 	return (0);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate static void
1637c478bd9Sstevel@tonic-gate setsignals(catch)
1647c478bd9Sstevel@tonic-gate void (*catch)();
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	signal(SIGHUP, catch);
1677c478bd9Sstevel@tonic-gate 	signal(SIGINT, catch);
1687c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, catch);
1697c478bd9Sstevel@tonic-gate 	signal(SIGTERM, catch);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate static void
openfail(int i)1727c478bd9Sstevel@tonic-gate openfail(int i)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	extern char *rterm;
1757c478bd9Sstevel@tonic-gate #if 0
1767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
1777c478bd9Sstevel@tonic-gate 		gettext("Timeout trying to open line(%s).\n"),
1787c478bd9Sstevel@tonic-gate 			rterm);
1797c478bd9Sstevel@tonic-gate #endif
1807c478bd9Sstevel@tonic-gate 	syslog(LOG_ERR, gettext("Timeout trying to open line(%s).\n"),
1817c478bd9Sstevel@tonic-gate 			rterm ? rterm : "");
1827c478bd9Sstevel@tonic-gate 	exit(1);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate static void
eof(void)1867c478bd9Sstevel@tonic-gate eof(void)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	extern FILE *fp;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s\r\n", gettext("<EOT>"));
1917c478bd9Sstevel@tonic-gate }
192