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 * 4. Neither the name of the University nor the names of its contributors
171590Srgrimes *    may be used to endorse or promote products derived from this software
181590Srgrimes *    without specific prior written permission.
191590Srgrimes *
201590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301590Srgrimes * SUCH DAMAGE.
311590Srgrimes */
321590Srgrimes
331590Srgrimes#ifndef lint
3428794Scharnierstatic const char copyright[] =
351590Srgrimes"@(#) Copyright (c) 1989, 1993\n\
361590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
3787682Smarkm#endif
381590Srgrimes
3991378Sdwmalone#if 0
401590Srgrimes#ifndef lint
4191378Sdwmalonestatic char sccsid[] = "@(#)write.c	8.1 (Berkeley) 6/6/93";
4228794Scharnier#endif
4391378Sdwmalone#endif
441590Srgrimes
4591378Sdwmalone#include <sys/cdefs.h>
4691378Sdwmalone__FBSDID("$FreeBSD$");
4791378Sdwmalone
481590Srgrimes#include <sys/param.h>
491590Srgrimes#include <sys/signal.h>
501590Srgrimes#include <sys/stat.h>
511590Srgrimes#include <sys/file.h>
521590Srgrimes#include <sys/time.h>
531590Srgrimes#include <ctype.h>
5428794Scharnier#include <err.h>
5529430Sache#include <locale.h>
5619190Salex#include <paths.h>
571590Srgrimes#include <pwd.h>
581590Srgrimes#include <stdio.h>
5991378Sdwmalone#include <stdlib.h>
601590Srgrimes#include <string.h>
6128794Scharnier#include <unistd.h>
62202200Sed#include <utmpx.h>
63231586Sglebius#include <wchar.h>
64231586Sglebius#include <wctype.h>
651590Srgrimes
6692922Simpvoid done(int);
6792922Simpvoid do_write(char *, char *, uid_t);
6892922Simpstatic void usage(void);
6992922Simpint term_chk(char *, int *, time_t *, int);
70231586Sglebiusvoid wr_fputs(wchar_t *s);
7192922Simpvoid search_utmp(char *, char *, char *, uid_t);
7292922Simpint utmp_chk(char *, char *);
731590Srgrimes
7428794Scharnierint
75102944Sdwmalonemain(int argc, char **argv)
761590Srgrimes{
771590Srgrimes	time_t atime;
781590Srgrimes	uid_t myuid;
791590Srgrimes	int msgsok, myttyfd;
8028794Scharnier	char tty[MAXPATHLEN], *mytty;
811590Srgrimes
8229430Sache	(void)setlocale(LC_CTYPE, "");
8329430Sache
8497454Stjr	while (getopt(argc, argv, "") != -1)
8597454Stjr		usage();
8697454Stjr	argc -= optind;
8797454Stjr	argv += optind;
8897454Stjr
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");
100173572Sjhb	if (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV)))
101173572Sjhb		mytty += strlen(_PATH_DEV);
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) {
11197454Stjr	case 1:
11297454Stjr		search_utmp(argv[0], tty, mytty, myuid);
1131590Srgrimes		do_write(tty, mytty, myuid);
1141590Srgrimes		break;
11597454Stjr	case 2:
11697454Stjr		if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV)))
11797454Stjr			argv[1] += strlen(_PATH_DEV);
11897454Stjr		if (utmp_chk(argv[0], argv[1]))
11997454Stjr			errx(1, "%s is not logged in on %s", argv[0], argv[1]);
12097454Stjr		if (term_chk(argv[1], &msgsok, &atime, 1))
1211590Srgrimes			exit(1);
12228794Scharnier		if (myuid && !msgsok)
12397454Stjr			errx(1, "%s has messages disabled on %s", argv[0], argv[1]);
12497454Stjr		do_write(argv[1], mytty, myuid);
1251590Srgrimes		break;
1261590Srgrimes	default:
12728794Scharnier		usage();
1281590Srgrimes	}
12929430Sache	done(0);
13028794Scharnier	return (0);
1311590Srgrimes}
1321590Srgrimes
13328794Scharnierstatic void
134102944Sdwmaloneusage(void)
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
145102944Sdwmaloneutmp_chk(char *user, char *tty)
1461590Srgrimes{
147200160Sed	struct utmpx lu, *u;
1481590Srgrimes
149200160Sed	strncpy(lu.ut_line, tty, sizeof lu.ut_line);
150200160Sed	setutxent();
151200160Sed	while ((u = getutxline(&lu)) != NULL)
152200160Sed		if (u->ut_type == USER_PROCESS &&
153200160Sed		    strcmp(user, u->ut_user) == 0) {
154200160Sed			endutxent();
1551590Srgrimes			return(0);
1561590Srgrimes		}
157200160Sed	endutxent();
1581590Srgrimes	return(1);
1591590Srgrimes}
1601590Srgrimes
1611590Srgrimes/*
1621590Srgrimes * search_utmp - search utmp for the "best" terminal to write to
1631590Srgrimes *
1641590Srgrimes * Ignores terminals with messages disabled, and of the rest, returns
1651590Srgrimes * the one with the most recent access time.  Returns as value the number
1661590Srgrimes * of the user's terminals with messages enabled, or -1 if the user is
1671590Srgrimes * not logged in at all.
1681590Srgrimes *
1691590Srgrimes * Special case for writing to yourself - ignore the terminal you're
1701590Srgrimes * writing from, unless that's the only terminal with messages enabled.
1711590Srgrimes */
17228794Scharniervoid
173102944Sdwmalonesearch_utmp(char *user, char *tty, char *mytty, uid_t myuid)
1741590Srgrimes{
175200160Sed	struct utmpx *u;
1761590Srgrimes	time_t bestatime, atime;
177200160Sed	int nloggedttys, nttys, msgsok, user_is_me;
1781590Srgrimes
1791590Srgrimes	nloggedttys = nttys = 0;
1801590Srgrimes	bestatime = 0;
1811590Srgrimes	user_is_me = 0;
182200160Sed
183200160Sed	setutxent();
184200160Sed	while ((u = getutxent()) != NULL)
185200160Sed		if (u->ut_type == USER_PROCESS &&
186200160Sed		    strcmp(user, u->ut_user) == 0) {
1871590Srgrimes			++nloggedttys;
188200160Sed			if (term_chk(u->ut_line, &msgsok, &atime, 0))
1891590Srgrimes				continue;	/* bad term? skip */
1901590Srgrimes			if (myuid && !msgsok)
1911590Srgrimes				continue;	/* skip ttys with msgs off */
192200160Sed			if (strcmp(u->ut_line, mytty) == 0) {
1931590Srgrimes				user_is_me = 1;
1941590Srgrimes				continue;	/* don't write to yourself */
1951590Srgrimes			}
1961590Srgrimes			++nttys;
1971590Srgrimes			if (atime > bestatime) {
1981590Srgrimes				bestatime = atime;
199200160Sed				(void)strlcpy(tty, u->ut_line, MAXPATHLEN);
2001590Srgrimes			}
2011590Srgrimes		}
202200160Sed	endutxent();
2031590Srgrimes
20428794Scharnier	if (nloggedttys == 0)
20528794Scharnier		errx(1, "%s is not logged in", user);
2061590Srgrimes	if (nttys == 0) {
2071590Srgrimes		if (user_is_me) {		/* ok, so write to yourself! */
208200160Sed			(void)strlcpy(tty, mytty, MAXPATHLEN);
2091590Srgrimes			return;
2101590Srgrimes		}
21128794Scharnier		errx(1, "%s has messages disabled", user);
2121590Srgrimes	} else if (nttys > 1) {
21328794Scharnier		warnx("%s is logged in more than once; writing to %s", user, tty);
2141590Srgrimes	}
2151590Srgrimes}
2161590Srgrimes
2171590Srgrimes/*
2181590Srgrimes * term_chk - check that a terminal exists, and get the message bit
2191590Srgrimes *     and the access time
2201590Srgrimes */
22128794Scharnierint
222102944Sdwmaloneterm_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
2231590Srgrimes{
2241590Srgrimes	struct stat s;
2251590Srgrimes	char path[MAXPATHLEN];
2261590Srgrimes
22719190Salex	(void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
2281590Srgrimes	if (stat(path, &s) < 0) {
2291590Srgrimes		if (showerror)
23028794Scharnier			warn("%s", path);
2311590Srgrimes		return(1);
2321590Srgrimes	}
2331590Srgrimes	*msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0;	/* group write bit */
2341590Srgrimes	*atimeP = s.st_atime;
2351590Srgrimes	return(0);
2361590Srgrimes}
2371590Srgrimes
2381590Srgrimes/*
2391590Srgrimes * do_write - actually make the connection
2401590Srgrimes */
24128794Scharniervoid
242102944Sdwmalonedo_write(char *tty, char *mytty, uid_t myuid)
2431590Srgrimes{
24487682Smarkm	const char *login;
24587682Smarkm	char *nows;
24687682Smarkm	struct passwd *pwd;
24728794Scharnier	time_t now;
248231586Sglebius	char path[MAXPATHLEN], host[MAXHOSTNAMELEN];
249231586Sglebius	wchar_t line[512];
2501590Srgrimes
251139718Scognet	/* Determine our login name before we reopen() stdout */
25248566Sbillf	if ((login = getlogin()) == NULL) {
25328794Scharnier		if ((pwd = getpwuid(myuid)))
2541590Srgrimes			login = pwd->pw_name;
2551590Srgrimes		else
2561590Srgrimes			login = "???";
25748566Sbillf	}
2581590Srgrimes
25919190Salex	(void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
26028794Scharnier	if ((freopen(path, "w", stdout)) == NULL)
26128794Scharnier		err(1, "%s", path);
2621590Srgrimes
2631590Srgrimes	(void)signal(SIGINT, done);
2641590Srgrimes	(void)signal(SIGHUP, done);
2651590Srgrimes
2661590Srgrimes	/* print greeting */
2671590Srgrimes	if (gethostname(host, sizeof(host)) < 0)
2681590Srgrimes		(void)strcpy(host, "???");
2691590Srgrimes	now = time((time_t *)NULL);
2701590Srgrimes	nows = ctime(&now);
2711590Srgrimes	nows[16] = '\0';
2721590Srgrimes	(void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
2731590Srgrimes	    login, host, mytty, nows + 11);
2741590Srgrimes
275231586Sglebius	while (fgetws(line, sizeof(line)/sizeof(wchar_t), stdin) != NULL)
2761590Srgrimes		wr_fputs(line);
2771590Srgrimes}
2781590Srgrimes
2791590Srgrimes/*
2801590Srgrimes * done - cleanup and exit
2811590Srgrimes */
2821590Srgrimesvoid
283102944Sdwmalonedone(int n __unused)
2841590Srgrimes{
2851590Srgrimes	(void)printf("EOF\r\n");
2861590Srgrimes	exit(0);
2871590Srgrimes}
2881590Srgrimes
2891590Srgrimes/*
2901590Srgrimes * wr_fputs - like fputs(), but makes control characters visible and
2911590Srgrimes *     turns \n into \r\n
2921590Srgrimes */
29328794Scharniervoid
294231586Sglebiuswr_fputs(wchar_t *s)
2951590Srgrimes{
2961590Srgrimes
297231586Sglebius#define	PUTC(c)	if (putwchar(c) == WEOF) err(1, NULL);
2981590Srgrimes
299231586Sglebius	for (; *s != L'\0'; ++s) {
300231586Sglebius		if (*s == L'\n') {
301231586Sglebius			PUTC(L'\r');
302231586Sglebius			PUTC(L'\n');
303231586Sglebius		} else if (iswprint(*s) || iswspace(*s)) {
304231586Sglebius			PUTC(*s);
305231586Sglebius		} else {
306231586Sglebius			wprintf(L"<0x%X>", *s);
30712097Sache		}
3081590Srgrimes	}
3091590Srgrimes	return;
3101590Srgrimes#undef PUTC
3111590Srgrimes}
312