xref: /onnv-gate/usr/src/lib/libmail/common/notifyu.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211219Sraf 
22*6812Sraf /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6812Sraf  * Use is subject to license terms.
25*6812Sraf  */
26*6812Sraf 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include "libmail.h"
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <fcntl.h>
360Sstevel@tonic-gate #include <utmpx.h>
370Sstevel@tonic-gate #include <syslog.h>
380Sstevel@tonic-gate #if !defined(__cplusplus) && !defined(c_plusplus)
390Sstevel@tonic-gate typedef void (*SIG_PF) (int);
400Sstevel@tonic-gate #endif
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <signal.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static SIG_PF catcher(void);
450Sstevel@tonic-gate 
catcher(void)460Sstevel@tonic-gate static SIG_PF catcher(void)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	/* do nothing, but allow the write() to break */
490Sstevel@tonic-gate 	return (0);
500Sstevel@tonic-gate }
510Sstevel@tonic-gate 
520Sstevel@tonic-gate void
notify(char * user,char * msg,int check_mesg_y,char * etcdir)530Sstevel@tonic-gate notify(char *user, char *msg, int check_mesg_y, char *etcdir)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	/* search the utmp file for this user */
560Sstevel@tonic-gate 	SIG_PF old;
570Sstevel@tonic-gate 	unsigned int oldalarm;
580Sstevel@tonic-gate 	struct utmpx utmpx, *putmpx = &utmpx;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	setutxent();
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	/* grab the tty name */
630Sstevel@tonic-gate 	while ((putmpx = getutxent()) != NULL) {
640Sstevel@tonic-gate 		if (strncmp(user, utmpx.ut_name,
650Sstevel@tonic-gate 		    sizeof (utmpx.ut_name)) == 0) {
660Sstevel@tonic-gate 			char tty[sizeof (utmpx.ut_line)+1];
670Sstevel@tonic-gate 			char dev[MAXFILENAME];
680Sstevel@tonic-gate 			FILE *port;
690Sstevel@tonic-gate 			size_t i;
700Sstevel@tonic-gate 			int fd;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 			for (i = 0; i < sizeof (utmpx.ut_line); i++)
730Sstevel@tonic-gate 				tty[i] = utmpx.ut_line[i];
740Sstevel@tonic-gate 			tty[i] = '\0';
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 			/* stick /dev/ in front */
770Sstevel@tonic-gate 			(void) sprintf(dev, "%s/dev/%s", etcdir, tty);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 			/* break out if write() to the tty hangs */
800Sstevel@tonic-gate 			old = (SIG_PF)signal(SIGALRM, (SIG_PF)catcher);
810Sstevel@tonic-gate 			oldalarm = alarm(300);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 			/* check if device is really a tty */
840Sstevel@tonic-gate 			if ((fd = open(dev, O_WRONLY|O_NOCTTY)) == -1) {
850Sstevel@tonic-gate 				(void) fprintf(stderr,
860Sstevel@tonic-gate 				    "Cannot open %s.\n", dev);
870Sstevel@tonic-gate 				continue;
880Sstevel@tonic-gate 			} else {
890Sstevel@tonic-gate 				if (!isatty(fd)) {
900Sstevel@tonic-gate 					(void) fprintf(stderr, "%s in utmpx is "
910Sstevel@tonic-gate 					    "not a tty\n", tty);
920Sstevel@tonic-gate 					openlog("mail", 0, LOG_AUTH);
930Sstevel@tonic-gate 					syslog(LOG_CRIT, "%s in utmp is "
940Sstevel@tonic-gate 					    "not a tty\n", tty);
950Sstevel@tonic-gate 					closelog();
960Sstevel@tonic-gate 					(void) close(fd);
970Sstevel@tonic-gate 					continue;
980Sstevel@tonic-gate 				}
990Sstevel@tonic-gate 			}
1000Sstevel@tonic-gate 			(void) close(fd);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 			/* write to the tty */
1030Sstevel@tonic-gate 			port = fopen(dev, "w");
1040Sstevel@tonic-gate 			if (port != 0) {
1050Sstevel@tonic-gate 				(void) fprintf(port, "\r\n%s\r\n", msg);
1060Sstevel@tonic-gate 				(void) fclose(port);
1070Sstevel@tonic-gate 			}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 			/* clean up our alarm */
1100Sstevel@tonic-gate 			(void) alarm(0);
1110Sstevel@tonic-gate 			(void) signal(SIGALRM, old);
1120Sstevel@tonic-gate 			(void) alarm(oldalarm);
1130Sstevel@tonic-gate 		}
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 	endutxent();
1160Sstevel@tonic-gate }
117