write.c revision 200160
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 200160 2009-12-05 20:22:26Z ed $");
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>
65200160Sed#define	_ULOG_POSIX_NAMES
66200160Sed#include <ulog.h>
6728794Scharnier#include <unistd.h>
681590Srgrimes
6992922Simpvoid done(int);
7092922Simpvoid do_write(char *, char *, uid_t);
7192922Simpstatic void usage(void);
7292922Simpint term_chk(char *, int *, time_t *, int);
7392922Simpvoid wr_fputs(unsigned char *s);
7492922Simpvoid search_utmp(char *, char *, char *, uid_t);
7592922Simpint utmp_chk(char *, char *);
761590Srgrimes
7728794Scharnierint
78102944Sdwmalonemain(int argc, char **argv)
791590Srgrimes{
801590Srgrimes	time_t atime;
811590Srgrimes	uid_t myuid;
821590Srgrimes	int msgsok, myttyfd;
8328794Scharnier	char tty[MAXPATHLEN], *mytty;
841590Srgrimes
8529430Sache	(void)setlocale(LC_CTYPE, "");
8629430Sache
8797454Stjr	while (getopt(argc, argv, "") != -1)
8897454Stjr		usage();
8997454Stjr	argc -= optind;
9097454Stjr	argv += optind;
9197454Stjr
921590Srgrimes	/* check that sender has write enabled */
931590Srgrimes	if (isatty(fileno(stdin)))
941590Srgrimes		myttyfd = fileno(stdin);
951590Srgrimes	else if (isatty(fileno(stdout)))
961590Srgrimes		myttyfd = fileno(stdout);
971590Srgrimes	else if (isatty(fileno(stderr)))
981590Srgrimes		myttyfd = fileno(stderr);
9928794Scharnier	else
10028794Scharnier		errx(1, "can't find your tty");
10128794Scharnier	if (!(mytty = ttyname(myttyfd)))
10228794Scharnier		errx(1, "can't find your tty's name");
103173572Sjhb	if (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV)))
104173572Sjhb		mytty += strlen(_PATH_DEV);
1051590Srgrimes	if (term_chk(mytty, &msgsok, &atime, 1))
1061590Srgrimes		exit(1);
10728794Scharnier	if (!msgsok)
10828794Scharnier		errx(1, "you have write permission turned off");
1091590Srgrimes
1101590Srgrimes	myuid = getuid();
1111590Srgrimes
1121590Srgrimes	/* check args */
1131590Srgrimes	switch (argc) {
11497454Stjr	case 1:
11597454Stjr		search_utmp(argv[0], tty, mytty, myuid);
1161590Srgrimes		do_write(tty, mytty, myuid);
1171590Srgrimes		break;
11897454Stjr	case 2:
11997454Stjr		if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV)))
12097454Stjr			argv[1] += strlen(_PATH_DEV);
12197454Stjr		if (utmp_chk(argv[0], argv[1]))
12297454Stjr			errx(1, "%s is not logged in on %s", argv[0], argv[1]);
12397454Stjr		if (term_chk(argv[1], &msgsok, &atime, 1))
1241590Srgrimes			exit(1);
12528794Scharnier		if (myuid && !msgsok)
12697454Stjr			errx(1, "%s has messages disabled on %s", argv[0], argv[1]);
12797454Stjr		do_write(argv[1], mytty, myuid);
1281590Srgrimes		break;
1291590Srgrimes	default:
13028794Scharnier		usage();
1311590Srgrimes	}
13229430Sache	done(0);
13328794Scharnier	return (0);
1341590Srgrimes}
1351590Srgrimes
13628794Scharnierstatic void
137102944Sdwmaloneusage(void)
13828794Scharnier{
13928794Scharnier	(void)fprintf(stderr, "usage: write user [tty]\n");
14028794Scharnier	exit(1);
14128794Scharnier}
14228794Scharnier
1431590Srgrimes/*
1441590Srgrimes * utmp_chk - checks that the given user is actually logged in on
1451590Srgrimes *     the given tty
1461590Srgrimes */
14728794Scharnierint
148102944Sdwmaloneutmp_chk(char *user, char *tty)
1491590Srgrimes{
150200160Sed	struct utmpx lu, *u;
1511590Srgrimes
152200160Sed	strncpy(lu.ut_line, tty, sizeof lu.ut_line);
153200160Sed	setutxent();
154200160Sed	while ((u = getutxline(&lu)) != NULL)
155200160Sed		if (u->ut_type == USER_PROCESS &&
156200160Sed		    strcmp(user, u->ut_user) == 0) {
157200160Sed			endutxent();
1581590Srgrimes			return(0);
1591590Srgrimes		}
160200160Sed	endutxent();
1611590Srgrimes	return(1);
1621590Srgrimes}
1631590Srgrimes
1641590Srgrimes/*
1651590Srgrimes * search_utmp - search utmp for the "best" terminal to write to
1661590Srgrimes *
1671590Srgrimes * Ignores terminals with messages disabled, and of the rest, returns
1681590Srgrimes * the one with the most recent access time.  Returns as value the number
1691590Srgrimes * of the user's terminals with messages enabled, or -1 if the user is
1701590Srgrimes * not logged in at all.
1711590Srgrimes *
1721590Srgrimes * Special case for writing to yourself - ignore the terminal you're
1731590Srgrimes * writing from, unless that's the only terminal with messages enabled.
1741590Srgrimes */
17528794Scharniervoid
176102944Sdwmalonesearch_utmp(char *user, char *tty, char *mytty, uid_t myuid)
1771590Srgrimes{
178200160Sed	struct utmpx *u;
1791590Srgrimes	time_t bestatime, atime;
180200160Sed	int nloggedttys, nttys, msgsok, user_is_me;
1811590Srgrimes
1821590Srgrimes	nloggedttys = nttys = 0;
1831590Srgrimes	bestatime = 0;
1841590Srgrimes	user_is_me = 0;
185200160Sed
186200160Sed	setutxent();
187200160Sed	while ((u = getutxent()) != NULL)
188200160Sed		if (u->ut_type == USER_PROCESS &&
189200160Sed		    strcmp(user, u->ut_user) == 0) {
1901590Srgrimes			++nloggedttys;
191200160Sed			if (term_chk(u->ut_line, &msgsok, &atime, 0))
1921590Srgrimes				continue;	/* bad term? skip */
1931590Srgrimes			if (myuid && !msgsok)
1941590Srgrimes				continue;	/* skip ttys with msgs off */
195200160Sed			if (strcmp(u->ut_line, mytty) == 0) {
1961590Srgrimes				user_is_me = 1;
1971590Srgrimes				continue;	/* don't write to yourself */
1981590Srgrimes			}
1991590Srgrimes			++nttys;
2001590Srgrimes			if (atime > bestatime) {
2011590Srgrimes				bestatime = atime;
202200160Sed				(void)strlcpy(tty, u->ut_line, MAXPATHLEN);
2031590Srgrimes			}
2041590Srgrimes		}
205200160Sed	endutxent();
2061590Srgrimes
20728794Scharnier	if (nloggedttys == 0)
20828794Scharnier		errx(1, "%s is not logged in", user);
2091590Srgrimes	if (nttys == 0) {
2101590Srgrimes		if (user_is_me) {		/* ok, so write to yourself! */
211200160Sed			(void)strlcpy(tty, mytty, MAXPATHLEN);
2121590Srgrimes			return;
2131590Srgrimes		}
21428794Scharnier		errx(1, "%s has messages disabled", user);
2151590Srgrimes	} else if (nttys > 1) {
21628794Scharnier		warnx("%s is logged in more than once; writing to %s", user, tty);
2171590Srgrimes	}
2181590Srgrimes}
2191590Srgrimes
2201590Srgrimes/*
2211590Srgrimes * term_chk - check that a terminal exists, and get the message bit
2221590Srgrimes *     and the access time
2231590Srgrimes */
22428794Scharnierint
225102944Sdwmaloneterm_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
2261590Srgrimes{
2271590Srgrimes	struct stat s;
2281590Srgrimes	char path[MAXPATHLEN];
2291590Srgrimes
23019190Salex	(void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
2311590Srgrimes	if (stat(path, &s) < 0) {
2321590Srgrimes		if (showerror)
23328794Scharnier			warn("%s", path);
2341590Srgrimes		return(1);
2351590Srgrimes	}
2361590Srgrimes	*msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0;	/* group write bit */
2371590Srgrimes	*atimeP = s.st_atime;
2381590Srgrimes	return(0);
2391590Srgrimes}
2401590Srgrimes
2411590Srgrimes/*
2421590Srgrimes * do_write - actually make the connection
2431590Srgrimes */
24428794Scharniervoid
245102944Sdwmalonedo_write(char *tty, char *mytty, uid_t myuid)
2461590Srgrimes{
24787682Smarkm	const char *login;
24887682Smarkm	char *nows;
24987682Smarkm	struct passwd *pwd;
25028794Scharnier	time_t now;
25128794Scharnier	char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
2521590Srgrimes
253139718Scognet	/* Determine our login name before we reopen() stdout */
25448566Sbillf	if ((login = getlogin()) == NULL) {
25528794Scharnier		if ((pwd = getpwuid(myuid)))
2561590Srgrimes			login = pwd->pw_name;
2571590Srgrimes		else
2581590Srgrimes			login = "???";
25948566Sbillf	}
2601590Srgrimes
26119190Salex	(void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
26228794Scharnier	if ((freopen(path, "w", stdout)) == NULL)
26328794Scharnier		err(1, "%s", path);
2641590Srgrimes
2651590Srgrimes	(void)signal(SIGINT, done);
2661590Srgrimes	(void)signal(SIGHUP, done);
2671590Srgrimes
2681590Srgrimes	/* print greeting */
2691590Srgrimes	if (gethostname(host, sizeof(host)) < 0)
2701590Srgrimes		(void)strcpy(host, "???");
2711590Srgrimes	now = time((time_t *)NULL);
2721590Srgrimes	nows = ctime(&now);
2731590Srgrimes	nows[16] = '\0';
2741590Srgrimes	(void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
2751590Srgrimes	    login, host, mytty, nows + 11);
2761590Srgrimes
2771590Srgrimes	while (fgets(line, sizeof(line), stdin) != NULL)
2781590Srgrimes		wr_fputs(line);
2791590Srgrimes}
2801590Srgrimes
2811590Srgrimes/*
2821590Srgrimes * done - cleanup and exit
2831590Srgrimes */
2841590Srgrimesvoid
285102944Sdwmalonedone(int n __unused)
2861590Srgrimes{
2871590Srgrimes	(void)printf("EOF\r\n");
2881590Srgrimes	exit(0);
2891590Srgrimes}
2901590Srgrimes
2911590Srgrimes/*
2921590Srgrimes * wr_fputs - like fputs(), but makes control characters visible and
2931590Srgrimes *     turns \n into \r\n
2941590Srgrimes */
29528794Scharniervoid
296102944Sdwmalonewr_fputs(unsigned char *s)
2971590Srgrimes{
2981590Srgrimes
29928794Scharnier#define	PUTC(c)	if (putchar(c) == EOF) err(1, NULL);
3001590Srgrimes
3011590Srgrimes	for (; *s != '\0'; ++s) {
30211916Sache		if (*s == '\n') {
3031590Srgrimes			PUTC('\r');
30429431Sache		} else if (((*s & 0x80) && *s < 0xA0) ||
30529431Sache			   /* disable upper controls */
30629433Sache			   (!isprint(*s) && !isspace(*s) &&
30729433Sache			    *s != '\a' && *s != '\b')
30829430Sache			  ) {
30912097Sache			if (*s & 0x80) {
31012097Sache				*s &= ~0x80;
31112097Sache				PUTC('M');
31212097Sache				PUTC('-');
31312097Sache			}
31412097Sache			if (iscntrl(*s)) {
31512097Sache				*s ^= 0x40;
31612097Sache				PUTC('^');
31712097Sache			}
31812097Sache		}
31912097Sache		PUTC(*s);
3201590Srgrimes	}
3211590Srgrimes	return;
3221590Srgrimes#undef PUTC
3231590Srgrimes}
324