write.c revision 91378
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1989, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 3. All advertising materials mentioning features or use of this software
171590Srgrimes *    must display the following acknowledgement:
181590Srgrimes *	This product includes software developed by the University of
191590Srgrimes *	California, Berkeley and its contributors.
201590Srgrimes * 4. Neither the name of the University nor the names of its contributors
211590Srgrimes *    may be used to endorse or promote products derived from this software
221590Srgrimes *    without specific prior written permission.
231590Srgrimes *
241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341590Srgrimes * SUCH DAMAGE.
351590Srgrimes */
361590Srgrimes
371590Srgrimes#ifndef lint
3828794Scharnierstatic const char copyright[] =
391590Srgrimes"@(#) Copyright (c) 1989, 1993\n\
401590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
4187682Smarkm#endif
421590Srgrimes
4391378Sdwmalone#if 0
441590Srgrimes#ifndef lint
4591378Sdwmalonestatic char sccsid[] = "@(#)write.c	8.1 (Berkeley) 6/6/93";
4628794Scharnier#endif
4791378Sdwmalone#endif
481590Srgrimes
4991378Sdwmalone#include <sys/cdefs.h>
5091378Sdwmalone__FBSDID("$FreeBSD: head/usr.bin/write/write.c 91378 2002-02-27 14:40:09Z dwmalone $");
5191378Sdwmalone
521590Srgrimes#include <sys/param.h>
531590Srgrimes#include <sys/signal.h>
541590Srgrimes#include <sys/stat.h>
551590Srgrimes#include <sys/file.h>
561590Srgrimes#include <sys/time.h>
571590Srgrimes#include <ctype.h>
5828794Scharnier#include <err.h>
5929430Sache#include <locale.h>
6019190Salex#include <paths.h>
611590Srgrimes#include <pwd.h>
621590Srgrimes#include <stdio.h>
6391378Sdwmalone#include <stdlib.h>
641590Srgrimes#include <string.h>
6528794Scharnier#include <unistd.h>
6628794Scharnier#include <utmp.h>
671590Srgrimes
6829430Sachevoid done __P((int));
6928794Scharniervoid do_write __P((char *, char *, uid_t));
7028794Scharnierstatic void usage __P((void));
7128794Scharnierint term_chk __P((char *, int *, time_t *, int));
7228794Scharniervoid wr_fputs __P((unsigned char *s));
7328794Scharniervoid search_utmp __P((char *, char *, char *, uid_t));
7428794Scharnierint utmp_chk __P((char *, char *));
751590Srgrimes
7628794Scharnierint
771590Srgrimesmain(argc, argv)
781590Srgrimes	int argc;
791590Srgrimes	char **argv;
801590Srgrimes{
811590Srgrimes	register char *cp;
821590Srgrimes	time_t atime;
831590Srgrimes	uid_t myuid;
841590Srgrimes	int msgsok, myttyfd;
8528794Scharnier	char tty[MAXPATHLEN], *mytty;
861590Srgrimes
8729430Sache	(void)setlocale(LC_CTYPE, "");
8829430Sache
891590Srgrimes	/* check that sender has write enabled */
901590Srgrimes	if (isatty(fileno(stdin)))
911590Srgrimes		myttyfd = fileno(stdin);
921590Srgrimes	else if (isatty(fileno(stdout)))
931590Srgrimes		myttyfd = fileno(stdout);
941590Srgrimes	else if (isatty(fileno(stderr)))
951590Srgrimes		myttyfd = fileno(stderr);
9628794Scharnier	else
9728794Scharnier		errx(1, "can't find your tty");
9828794Scharnier	if (!(mytty = ttyname(myttyfd)))
9928794Scharnier		errx(1, "can't find your tty's name");
10028794Scharnier	if ((cp = rindex(mytty, '/')))
1011590Srgrimes		mytty = cp + 1;
1021590Srgrimes	if (term_chk(mytty, &msgsok, &atime, 1))
1031590Srgrimes		exit(1);
10428794Scharnier	if (!msgsok)
10528794Scharnier		errx(1, "you have write permission turned off");
1061590Srgrimes
1071590Srgrimes	myuid = getuid();
1081590Srgrimes
1091590Srgrimes	/* check args */
1101590Srgrimes	switch (argc) {
1111590Srgrimes	case 2:
1121590Srgrimes		search_utmp(argv[1], tty, mytty, myuid);
1131590Srgrimes		do_write(tty, mytty, myuid);
1141590Srgrimes		break;
1151590Srgrimes	case 3:
11619190Salex		if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV)))
11719193Salex			argv[2] += strlen(_PATH_DEV);
11828794Scharnier		if (utmp_chk(argv[1], argv[2]))
11928794Scharnier			errx(1, "%s is not logged in on %s", argv[1], argv[2]);
1201590Srgrimes		if (term_chk(argv[2], &msgsok, &atime, 1))
1211590Srgrimes			exit(1);
12228794Scharnier		if (myuid && !msgsok)
12328794Scharnier			errx(1, "%s has messages disabled on %s", argv[1], argv[2]);
1241590Srgrimes		do_write(argv[2], mytty, myuid);
1251590Srgrimes		break;
1261590Srgrimes	default:
12728794Scharnier		usage();
1281590Srgrimes	}
12929430Sache	done(0);
13028794Scharnier	return (0);
1311590Srgrimes}
1321590Srgrimes
13328794Scharnierstatic void
13428794Scharnierusage()
13528794Scharnier{
13628794Scharnier	(void)fprintf(stderr, "usage: write user [tty]\n");
13728794Scharnier	exit(1);
13828794Scharnier}
13928794Scharnier
1401590Srgrimes/*
1411590Srgrimes * utmp_chk - checks that the given user is actually logged in on
1421590Srgrimes *     the given tty
1431590Srgrimes */
14428794Scharnierint
1451590Srgrimesutmp_chk(user, tty)
1461590Srgrimes	char *user, *tty;
1471590Srgrimes{
1481590Srgrimes	struct utmp u;
1491590Srgrimes	int ufd;
1501590Srgrimes
1511590Srgrimes	if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
1521590Srgrimes		return(0);	/* ignore error, shouldn't happen anyway */
1531590Srgrimes
1541590Srgrimes	while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
1551590Srgrimes		if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
1561590Srgrimes		    strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
1571590Srgrimes			(void)close(ufd);
1581590Srgrimes			return(0);
1591590Srgrimes		}
1601590Srgrimes
1611590Srgrimes	(void)close(ufd);
1621590Srgrimes	return(1);
1631590Srgrimes}
1641590Srgrimes
1651590Srgrimes/*
1661590Srgrimes * search_utmp - search utmp for the "best" terminal to write to
1671590Srgrimes *
1681590Srgrimes * Ignores terminals with messages disabled, and of the rest, returns
1691590Srgrimes * the one with the most recent access time.  Returns as value the number
1701590Srgrimes * of the user's terminals with messages enabled, or -1 if the user is
1711590Srgrimes * not logged in at all.
1721590Srgrimes *
1731590Srgrimes * Special case for writing to yourself - ignore the terminal you're
1741590Srgrimes * writing from, unless that's the only terminal with messages enabled.
1751590Srgrimes */
17628794Scharniervoid
1771590Srgrimessearch_utmp(user, tty, mytty, myuid)
1781590Srgrimes	char *user, *tty, *mytty;
1791590Srgrimes	uid_t myuid;
1801590Srgrimes{
1811590Srgrimes	struct utmp u;
1821590Srgrimes	time_t bestatime, atime;
1831590Srgrimes	int ufd, nloggedttys, nttys, msgsok, user_is_me;
1841590Srgrimes	char atty[UT_LINESIZE + 1];
1851590Srgrimes
18628794Scharnier	if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
18728794Scharnier		err(1, "utmp");
1881590Srgrimes
1891590Srgrimes	nloggedttys = nttys = 0;
1901590Srgrimes	bestatime = 0;
1911590Srgrimes	user_is_me = 0;
1921590Srgrimes	while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
1931590Srgrimes		if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
1941590Srgrimes			++nloggedttys;
1951590Srgrimes			(void)strncpy(atty, u.ut_line, UT_LINESIZE);
1961590Srgrimes			atty[UT_LINESIZE] = '\0';
1971590Srgrimes			if (term_chk(atty, &msgsok, &atime, 0))
1981590Srgrimes				continue;	/* bad term? skip */
1991590Srgrimes			if (myuid && !msgsok)
2001590Srgrimes				continue;	/* skip ttys with msgs off */
2011590Srgrimes			if (strcmp(atty, mytty) == 0) {
2021590Srgrimes				user_is_me = 1;
2031590Srgrimes				continue;	/* don't write to yourself */
2041590Srgrimes			}
2051590Srgrimes			++nttys;
2061590Srgrimes			if (atime > bestatime) {
2071590Srgrimes				bestatime = atime;
2081590Srgrimes				(void)strcpy(tty, atty);
2091590Srgrimes			}
2101590Srgrimes		}
2111590Srgrimes
2121590Srgrimes	(void)close(ufd);
21328794Scharnier	if (nloggedttys == 0)
21428794Scharnier		errx(1, "%s is not logged in", user);
2151590Srgrimes	if (nttys == 0) {
2161590Srgrimes		if (user_is_me) {		/* ok, so write to yourself! */
2171590Srgrimes			(void)strcpy(tty, mytty);
2181590Srgrimes			return;
2191590Srgrimes		}
22028794Scharnier		errx(1, "%s has messages disabled", user);
2211590Srgrimes	} else if (nttys > 1) {
22228794Scharnier		warnx("%s is logged in more than once; writing to %s", user, tty);
2231590Srgrimes	}
2241590Srgrimes}
2251590Srgrimes
2261590Srgrimes/*
2271590Srgrimes * term_chk - check that a terminal exists, and get the message bit
2281590Srgrimes *     and the access time
2291590Srgrimes */
23028794Scharnierint
2311590Srgrimesterm_chk(tty, msgsokP, atimeP, showerror)
2321590Srgrimes	char *tty;
2331590Srgrimes	int *msgsokP, showerror;
2341590Srgrimes	time_t *atimeP;
2351590Srgrimes{
2361590Srgrimes	struct stat s;
2371590Srgrimes	char path[MAXPATHLEN];
2381590Srgrimes
23919190Salex	(void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
2401590Srgrimes	if (stat(path, &s) < 0) {
2411590Srgrimes		if (showerror)
24228794Scharnier			warn("%s", path);
2431590Srgrimes		return(1);
2441590Srgrimes	}
2451590Srgrimes	*msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0;	/* group write bit */
2461590Srgrimes	*atimeP = s.st_atime;
2471590Srgrimes	return(0);
2481590Srgrimes}
2491590Srgrimes
2501590Srgrimes/*
2511590Srgrimes * do_write - actually make the connection
2521590Srgrimes */
25328794Scharniervoid
2541590Srgrimesdo_write(tty, mytty, myuid)
2551590Srgrimes	char *tty, *mytty;
2561590Srgrimes	uid_t myuid;
2571590Srgrimes{
25887682Smarkm	const char *login;
25987682Smarkm	char *nows;
26087682Smarkm	struct passwd *pwd;
26128794Scharnier	time_t now;
26228794Scharnier	char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
2631590Srgrimes
2641590Srgrimes	/* Determine our login name before the we reopen() stdout */
26548566Sbillf	if ((login = getlogin()) == NULL) {
26628794Scharnier		if ((pwd = getpwuid(myuid)))
2671590Srgrimes			login = pwd->pw_name;
2681590Srgrimes		else
2691590Srgrimes			login = "???";
27048566Sbillf	}
2711590Srgrimes
27219190Salex	(void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
27328794Scharnier	if ((freopen(path, "w", stdout)) == NULL)
27428794Scharnier		err(1, "%s", path);
2751590Srgrimes
2761590Srgrimes	(void)signal(SIGINT, done);
2771590Srgrimes	(void)signal(SIGHUP, done);
2781590Srgrimes
2791590Srgrimes	/* print greeting */
2801590Srgrimes	if (gethostname(host, sizeof(host)) < 0)
2811590Srgrimes		(void)strcpy(host, "???");
2821590Srgrimes	now = time((time_t *)NULL);
2831590Srgrimes	nows = ctime(&now);
2841590Srgrimes	nows[16] = '\0';
2851590Srgrimes	(void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
2861590Srgrimes	    login, host, mytty, nows + 11);
2871590Srgrimes
2881590Srgrimes	while (fgets(line, sizeof(line), stdin) != NULL)
2891590Srgrimes		wr_fputs(line);
2901590Srgrimes}
2911590Srgrimes
2921590Srgrimes/*
2931590Srgrimes * done - cleanup and exit
2941590Srgrimes */
2951590Srgrimesvoid
29629430Sachedone(n)
29787682Smarkm	int n __unused;
2981590Srgrimes{
2991590Srgrimes	(void)printf("EOF\r\n");
3001590Srgrimes	exit(0);
3011590Srgrimes}
3021590Srgrimes
3031590Srgrimes/*
3041590Srgrimes * wr_fputs - like fputs(), but makes control characters visible and
3051590Srgrimes *     turns \n into \r\n
3061590Srgrimes */
30728794Scharniervoid
3081590Srgrimeswr_fputs(s)
30987682Smarkm	unsigned char *s;
3101590Srgrimes{
3111590Srgrimes
31228794Scharnier#define	PUTC(c)	if (putchar(c) == EOF) err(1, NULL);
3131590Srgrimes
3141590Srgrimes	for (; *s != '\0'; ++s) {
31511916Sache		if (*s == '\n') {
3161590Srgrimes			PUTC('\r');
31729431Sache		} else if (((*s & 0x80) && *s < 0xA0) ||
31829431Sache			   /* disable upper controls */
31929433Sache			   (!isprint(*s) && !isspace(*s) &&
32029433Sache			    *s != '\a' && *s != '\b')
32129430Sache			  ) {
32212097Sache			if (*s & 0x80) {
32312097Sache				*s &= ~0x80;
32412097Sache				PUTC('M');
32512097Sache				PUTC('-');
32612097Sache			}
32712097Sache			if (iscntrl(*s)) {
32812097Sache				*s ^= 0x40;
32912097Sache				PUTC('^');
33012097Sache			}
33112097Sache		}
33212097Sache		PUTC(*s);
3331590Srgrimes	}
3341590Srgrimes	return;
3351590Srgrimes#undef PUTC
3361590Srgrimes}
337