xref: /onnv-gate/usr/src/cmd/krb5/kwarn/kwarnd_send.c (revision 1812:f70c0a56af3d)
10Sstevel@tonic-gate /*
2*1812Sgtb  * CDDL HEADER START
3*1812Sgtb  *
4*1812Sgtb  * The contents of this file are subject to the terms of the
5*1812Sgtb  * Common Development and Distribution License (the "License").
6*1812Sgtb  * You may not use this file except in compliance with the License.
7*1812Sgtb  *
8*1812Sgtb  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1812Sgtb  * or http://www.opensolaris.org/os/licensing.
10*1812Sgtb  * See the License for the specific language governing permissions
11*1812Sgtb  * and limitations under the License.
12*1812Sgtb  *
13*1812Sgtb  * When distributing Covered Code, include this CDDL HEADER in each
14*1812Sgtb  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1812Sgtb  * If applicable, add the following below this CDDL HEADER, with the
16*1812Sgtb  * fields enclosed by brackets "[]" replaced with your own identifying
17*1812Sgtb  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1812Sgtb  *
19*1812Sgtb  * CDDL HEADER END
20*1812Sgtb  */
21*1812Sgtb /*
22*1812Sgtb  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include	<ctype.h>
290Sstevel@tonic-gate #include	<string.h>
300Sstevel@tonic-gate #include	<stdio.h>
310Sstevel@tonic-gate #include	<signal.h>
320Sstevel@tonic-gate #include	<sys/wait.h>
330Sstevel@tonic-gate #include	<sys/types.h>
340Sstevel@tonic-gate #include	<sys/stat.h>
350Sstevel@tonic-gate #include	<stdlib.h>
360Sstevel@tonic-gate #include	<unistd.h>
370Sstevel@tonic-gate #include	<time.h>
380Sstevel@tonic-gate #include	<utmpx.h>
390Sstevel@tonic-gate #include	<pwd.h>
400Sstevel@tonic-gate #include	<fcntl.h>
410Sstevel@tonic-gate #include	<stdarg.h>
420Sstevel@tonic-gate #include	<locale.h>
430Sstevel@tonic-gate #include	<stdlib.h>
440Sstevel@tonic-gate #include	<limits.h>
450Sstevel@tonic-gate #include	<wctype.h>
460Sstevel@tonic-gate #include	<errno.h>
470Sstevel@tonic-gate #include	<syslog.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #define		TRUE	1
500Sstevel@tonic-gate #define		FALSE	0
510Sstevel@tonic-gate #define		FAILURE	-1
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  *	DATE-TIME format
540Sstevel@tonic-gate  *  %a	abbreviated weekday name
550Sstevel@tonic-gate  *  %b  abbreviated month name
560Sstevel@tonic-gate  *  %e  day of month
570Sstevel@tonic-gate  *  %H  hour - 24 hour clock
580Sstevel@tonic-gate  *  %M  minute
590Sstevel@tonic-gate  *  %S  second
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate 
63*1812Sgtb extern char myhostname[];
64*1812Sgtb extern char progname[];
65*1812Sgtb 
66*1812Sgtb 
670Sstevel@tonic-gate static void openfail(int);
680Sstevel@tonic-gate static void eof(void);
690Sstevel@tonic-gate static void setsignals(void (*)());
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static FILE	*fp;	/* File pointer for receipient's terminal */
720Sstevel@tonic-gate static char *rterm; /* Pointer to receipient's terminal */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate int
warn_send(char * receipient,char * msg)750Sstevel@tonic-gate warn_send(char *receipient, char *msg)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	register struct utmpx *ubuf;
780Sstevel@tonic-gate 	static char rterminal[] = "/dev/\0 2345678901";
790Sstevel@tonic-gate 	extern FILE *fp;
800Sstevel@tonic-gate 	time_t tod;
810Sstevel@tonic-gate 	char time_buf[40];
820Sstevel@tonic-gate 	register int bad = 0;
830Sstevel@tonic-gate 	char	*rcp1, *rcp2, *rcp3;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
860Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
870Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
880Sstevel@tonic-gate #endif
890Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*	Set "rterm" to location where receipient's terminal will go.	*/
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	rterm = &rterminal[sizeof ("/dev/") - 1];
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * strip any realm or instance from principal so we can match against unix
980Sstevel@tonic-gate  * userid.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate 	rcp1 = strdup(receipient);
1010Sstevel@tonic-gate 	rcp2 = strtok(rcp1, "@");
1020Sstevel@tonic-gate 	rcp3 = strtok(rcp2, "/");
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  *	Scan through the "utmpx" file for the
1060Sstevel@tonic-gate  *	entry for the person we want to send to.
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	setutxent();
1100Sstevel@tonic-gate 	while ((ubuf = getutxent()) != NULL) {
1110Sstevel@tonic-gate 		if (ubuf->ut_type == USER_PROCESS) {
1120Sstevel@tonic-gate 			if (strncmp(rcp3, ubuf->ut_user,
1130Sstevel@tonic-gate 				sizeof (ubuf->ut_user)) == 0) {
1140Sstevel@tonic-gate 				strncpy(rterm, &ubuf->ut_line[0],
1150Sstevel@tonic-gate 					sizeof (ubuf->ut_line)+1);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /*	Try to open up the line to the receipient's terminal.		*/
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 				signal(SIGALRM, openfail);
1200Sstevel@tonic-gate 				alarm(5);
1210Sstevel@tonic-gate 				fp = fopen(&rterminal[0], "w");
1220Sstevel@tonic-gate 				alarm(0);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*	Catch signals SIGHUP, SIGINT, SIGQUIT, and SIGTERM, and send	*/
1250Sstevel@tonic-gate /*	<EOT> message to receipient.			*/
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 				setsignals(eof);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*	Get the time of day, convert it to a string and throw away the	*/
1300Sstevel@tonic-gate /*	year information at the end of the string.			*/
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 				time(&tod);
1330Sstevel@tonic-gate 				cftime(time_buf, "%c", &tod);
1340Sstevel@tonic-gate 				(void) fprintf(fp, gettext(
135*1812Sgtb 	    "\r\n\007\007\007\tMessage from %s@%s [ %s ] ...\r\n"),
136*1812Sgtb 					    progname, myhostname, time_buf);
1370Sstevel@tonic-gate 				sleep(1);
138*1812Sgtb 				fprintf(fp, gettext("\r\nMessage to %s"), msg);
1390Sstevel@tonic-gate 				fflush(fp);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*	Since "end of file" received, send <EOT> message to receipient.	*/
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 				eof();
1440Sstevel@tonic-gate 				fclose(fp);
1450Sstevel@tonic-gate 			}
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 	free(rcp1);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /*	Did we find a place to talk to?  If we were looking for a */
1520Sstevel@tonic-gate /*	specific spot and didn't find it, complain and log it. */
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if (*rterm == '\0')
1550Sstevel@tonic-gate 		if (bad > 0) {
1560Sstevel@tonic-gate 			(void) syslog(LOG_ERR, gettext("no place to send.\n"));
1570Sstevel@tonic-gate 			return (1);
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	endutxent();
1610Sstevel@tonic-gate 	return (0);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static void
1650Sstevel@tonic-gate setsignals(catch)
1660Sstevel@tonic-gate void (*catch)();
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	signal(SIGHUP, catch);
1690Sstevel@tonic-gate 	signal(SIGINT, catch);
1700Sstevel@tonic-gate 	signal(SIGQUIT, catch);
1710Sstevel@tonic-gate 	signal(SIGTERM, catch);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate static void
openfail(int i)1740Sstevel@tonic-gate openfail(int i)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	extern char *rterm;
1770Sstevel@tonic-gate #if 0
1780Sstevel@tonic-gate 	(void) fprintf(stderr,
1790Sstevel@tonic-gate 		gettext("Timeout trying to open line(%s).\n"),
1800Sstevel@tonic-gate 			rterm);
1810Sstevel@tonic-gate #endif
1820Sstevel@tonic-gate 	syslog(LOG_ERR, gettext("Timeout trying to open line(%s).\n"),
1830Sstevel@tonic-gate 			rterm ? rterm : "");
1840Sstevel@tonic-gate 	exit(1);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate static void
eof(void)1880Sstevel@tonic-gate eof(void)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	extern FILE *fp;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	(void) fprintf(fp, "%s\r\n", gettext("<EOT>"));
1930Sstevel@tonic-gate }
194