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