notifyu.c revision 1219:f89f56c2d9ac
126219Swpaul/*
226219Swpaul * CDDL HEADER START
326219Swpaul *
426219Swpaul * The contents of this file are subject to the terms of the
526219Swpaul * Common Development and Distribution License, Version 1.0 only
626219Swpaul * (the "License").  You may not use this file except in compliance
726219Swpaul * with the License.
826219Swpaul *
926219Swpaul * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1026219Swpaul * or http://www.opensolaris.org/os/licensing.
1126219Swpaul * See the License for the specific language governing permissions
1226219Swpaul * and limitations under the License.
1326219Swpaul *
1426219Swpaul * When distributing Covered Code, include this CDDL HEADER in each
1526219Swpaul * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1626219Swpaul * If applicable, add the following below this CDDL HEADER, with the
1726219Swpaul * fields enclosed by brackets "[]" replaced with your own identifying
1826219Swpaul * information: Portions Copyright [yyyy] [name of copyright owner]
1926219Swpaul *
2026219Swpaul * CDDL HEADER END
2126219Swpaul */
2226219Swpaul
2326219Swpaul/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2426219Swpaul/*	  All Rights Reserved  	*/
2526219Swpaul
2626219Swpaul/*
2726219Swpaul * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2826219Swpaul * Use is subject to license terms.
2926219Swpaul */
3026219Swpaul
3126219Swpaul#pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.4	*/
3226219Swpaul/*LINTLIBRARY*/
3374462Salfred
3426219Swpaul#include "c_synonyms.h"
3526219Swpaul#include "libmail.h"
3626219Swpaul#include <sys/types.h>
3726219Swpaul#include <sys/stat.h>
3826219Swpaul#include <fcntl.h>
3926219Swpaul#include <utmpx.h>
4074462Salfred#include <syslog.h>
4126219Swpaul#if !defined(__cplusplus) && !defined(c_plusplus)
4226219Swpaultypedef void (*SIG_PF) (int);
4326219Swpaul#endif
4474462Salfred#include <unistd.h>
4526219Swpaul#include <signal.h>
4626219Swpaul
4726219Swpaulstatic SIG_PF catcher(void);
4826219Swpaul
4926219Swpaulstatic SIG_PF catcher(void)
5026219Swpaul{
5126219Swpaul	/* do nothing, but allow the write() to break */
5226219Swpaul	return (0);
5326219Swpaul}
5426219Swpaul
5526219Swpaulvoid
5626219Swpaulnotify(char *user, char *msg, int check_mesg_y, char *etcdir)
5726219Swpaul{
5826219Swpaul	/* search the utmp file for this user */
5926219Swpaul	SIG_PF old;
6026219Swpaul	unsigned int oldalarm;
6126219Swpaul	struct utmpx utmpx, *putmpx = &utmpx;
6226219Swpaul
6326219Swpaul	setutxent();
6426219Swpaul
6526219Swpaul	/* grab the tty name */
6626219Swpaul	while ((putmpx = getutxent()) != NULL) {
6726219Swpaul		if (strncmp(user, utmpx.ut_name,
6826219Swpaul		    sizeof (utmpx.ut_name)) == 0) {
6926219Swpaul			char tty[sizeof (utmpx.ut_line)+1];
7026219Swpaul			char dev[MAXFILENAME];
7126219Swpaul			FILE *port;
7226219Swpaul			size_t i;
7326219Swpaul			int fd;
7490271Salfred
7526219Swpaul			for (i = 0; i < sizeof (utmpx.ut_line); i++)
7626219Swpaul				tty[i] = utmpx.ut_line[i];
7726219Swpaul			tty[i] = '\0';
7826219Swpaul
7926219Swpaul			/* stick /dev/ in front */
8026219Swpaul			(void) sprintf(dev, "%s/dev/%s", etcdir, tty);
81
82			/* break out if write() to the tty hangs */
83			old = (SIG_PF)signal(SIGALRM, (SIG_PF)catcher);
84			oldalarm = alarm(300);
85
86			/* check if device is really a tty */
87			if ((fd = open(dev, O_WRONLY|O_NOCTTY)) == -1) {
88				(void) fprintf(stderr,
89				    "Cannot open %s.\n", dev);
90				continue;
91			} else {
92				if (!isatty(fd)) {
93					(void) fprintf(stderr, "%s in utmpx is "
94					    "not a tty\n", tty);
95					openlog("mail", 0, LOG_AUTH);
96					syslog(LOG_CRIT, "%s in utmp is "
97					    "not a tty\n", tty);
98					closelog();
99					(void) close(fd);
100					continue;
101				}
102			}
103			(void) close(fd);
104
105			/* write to the tty */
106			port = fopen(dev, "w");
107			if (port != 0) {
108				(void) fprintf(port, "\r\n%s\r\n", msg);
109				(void) fclose(port);
110			}
111
112			/* clean up our alarm */
113			(void) alarm(0);
114			(void) signal(SIGALRM, old);
115			(void) alarm(oldalarm);
116		}
117	}
118	endutxent();
119}
120