write.c revision 12097
125401Sjb/*
250476Speter * Copyright (c) 1989, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
5211774Simp * This code is derived from software contributed to Berkeley by
61573Srgrimes * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
7250883Sed *
8250883Sed * Redistribution and use in source and binary forms, with or without
9102697Stjr * modification, are permitted provided that the following conditions
10102697Stjr * are met:
11250883Sed * 1. Redistributions of source code must retain the above copyright
12132497Stjr *    notice, this list of conditions and the following disclaimer.
13139922Stjr * 2. Redistributions in binary form must reproduce the above copyright
14139922Stjr *    notice, this list of conditions and the following disclaimer in the
15136609Stjr *    documentation and/or other materials provided with the distribution.
16121845Stjr * 3. All advertising materials mentioning features or use of this software
17136604Stjr *    must display the following acknowledgement:
18132497Stjr *	This product includes software developed by the University of
19112177Stjr *	California, Berkeley and its contributors.
20112177Stjr * 4. Neither the name of the University nor the names of its contributors
21103218Stjr *    may be used to endorse or promote products derived from this software
22103793Stjr *    without specific prior written permission.
23227753Stheraven *
24227753Stheraven * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26156613Sdeischen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27156613Sdeischen * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28101305Sbde * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29232935Stheraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30101305Sbde * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31127608Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32127608Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33238781Sissyl0 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34238781Sissyl0 * SUCH DAMAGE.
35123222Stjr */
36136609Stjr
37123222Stjr#ifndef lint
38139922Stjrstatic char copyright[] =
39104406Stjr"@(#) Copyright (c) 1989, 1993\n\
40104406Stjr	The Regents of the University of California.  All rights reserved.\n";
41103218Stjr#endif /* not lint */
42123222Stjr
43227753Stheraven#ifndef lint
44227753Stheravenstatic char sccsid[] = "@(#)write.c	8.1 (Berkeley) 6/6/93";
45227753Stheraven#endif /* not lint */
46136604Stjr
4730624Sbde#include <sys/param.h>
48101305Sbde#include <sys/signal.h>
4989255Sbde#include <sys/stat.h>
50238919Sissyl0#include <sys/file.h>
51238919Sissyl0#include <sys/time.h>
52238919Sissyl0#include <utmp.h>
53238919Sissyl0#include <ctype.h>
54238919Sissyl0#include <pwd.h>
55101408Stjr#include <stdio.h>
56101408Stjr#include <string.h>
57127590Stjr
58101408Stjrextern int errno;
59101408Stjr
60101408Stjrmain(argc, argv)
61101408Stjr	int argc;
62238781Sissyl0	char **argv;
63238781Sissyl0{
64238781Sissyl0	register char *cp;
65238781Sissyl0	time_t atime;
66238781Sissyl0	uid_t myuid;
67238781Sissyl0	int msgsok, myttyfd;
68238781Sissyl0	char tty[MAXPATHLEN], *mytty, *ttyname();
69238781Sissyl0	void done();
70238781Sissyl0
71238781Sissyl0	/* check that sender has write enabled */
72238781Sissyl0	if (isatty(fileno(stdin)))
73238781Sissyl0		myttyfd = fileno(stdin);
7489255Sbde	else if (isatty(fileno(stdout)))
75250883Sed		myttyfd = fileno(stdout);
76132497Stjr	else if (isatty(fileno(stderr)))
77250883Sed		myttyfd = fileno(stderr);
78132497Stjr	else {
79112177Stjr		(void)fprintf(stderr, "write: can't find your tty\n");
80103793Stjr		exit(1);
81103793Stjr	}
82101313Stjr	if (!(mytty = ttyname(myttyfd))) {
83101253Stjr		(void)fprintf(stderr, "write: can't find your tty's name\n");
84		exit(1);
85	}
86	if (cp = rindex(mytty, '/'))
87		mytty = cp + 1;
88	if (term_chk(mytty, &msgsok, &atime, 1))
89		exit(1);
90	if (!msgsok) {
91		(void)fprintf(stderr,
92		    "write: you have write permission turned off.\n");
93		exit(1);
94	}
95
96	myuid = getuid();
97
98	/* check args */
99	switch (argc) {
100	case 2:
101		search_utmp(argv[1], tty, mytty, myuid);
102		do_write(tty, mytty, myuid);
103		break;
104	case 3:
105		if (!strncmp(argv[2], "/dev/", 5))
106			argv[2] += 5;
107		if (utmp_chk(argv[1], argv[2])) {
108			(void)fprintf(stderr,
109			    "write: %s is not logged in on %s.\n",
110			    argv[1], argv[2]);
111			exit(1);
112		}
113		if (term_chk(argv[2], &msgsok, &atime, 1))
114			exit(1);
115		if (myuid && !msgsok) {
116			(void)fprintf(stderr,
117			    "write: %s has messages disabled on %s\n",
118			    argv[1], argv[2]);
119			exit(1);
120		}
121		do_write(argv[2], mytty, myuid);
122		break;
123	default:
124		(void)fprintf(stderr, "usage: write user [tty]\n");
125		exit(1);
126	}
127	done();
128	/* NOTREACHED */
129}
130
131/*
132 * utmp_chk - checks that the given user is actually logged in on
133 *     the given tty
134 */
135utmp_chk(user, tty)
136	char *user, *tty;
137{
138	struct utmp u;
139	int ufd;
140
141	if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
142		return(0);	/* ignore error, shouldn't happen anyway */
143
144	while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
145		if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
146		    strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
147			(void)close(ufd);
148			return(0);
149		}
150
151	(void)close(ufd);
152	return(1);
153}
154
155/*
156 * search_utmp - search utmp for the "best" terminal to write to
157 *
158 * Ignores terminals with messages disabled, and of the rest, returns
159 * the one with the most recent access time.  Returns as value the number
160 * of the user's terminals with messages enabled, or -1 if the user is
161 * not logged in at all.
162 *
163 * Special case for writing to yourself - ignore the terminal you're
164 * writing from, unless that's the only terminal with messages enabled.
165 */
166search_utmp(user, tty, mytty, myuid)
167	char *user, *tty, *mytty;
168	uid_t myuid;
169{
170	struct utmp u;
171	time_t bestatime, atime;
172	int ufd, nloggedttys, nttys, msgsok, user_is_me;
173	char atty[UT_LINESIZE + 1];
174
175	if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) {
176		perror("utmp");
177		exit(1);
178	}
179
180	nloggedttys = nttys = 0;
181	bestatime = 0;
182	user_is_me = 0;
183	while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
184		if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
185			++nloggedttys;
186			(void)strncpy(atty, u.ut_line, UT_LINESIZE);
187			atty[UT_LINESIZE] = '\0';
188			if (term_chk(atty, &msgsok, &atime, 0))
189				continue;	/* bad term? skip */
190			if (myuid && !msgsok)
191				continue;	/* skip ttys with msgs off */
192			if (strcmp(atty, mytty) == 0) {
193				user_is_me = 1;
194				continue;	/* don't write to yourself */
195			}
196			++nttys;
197			if (atime > bestatime) {
198				bestatime = atime;
199				(void)strcpy(tty, atty);
200			}
201		}
202
203	(void)close(ufd);
204	if (nloggedttys == 0) {
205		(void)fprintf(stderr, "write: %s is not logged in\n", user);
206		exit(1);
207	}
208	if (nttys == 0) {
209		if (user_is_me) {		/* ok, so write to yourself! */
210			(void)strcpy(tty, mytty);
211			return;
212		}
213		(void)fprintf(stderr,
214		    "write: %s has messages disabled\n", user);
215		exit(1);
216	} else if (nttys > 1) {
217		(void)fprintf(stderr,
218		    "write: %s is logged in more than once; writing to %s\n",
219		    user, tty);
220	}
221}
222
223/*
224 * term_chk - check that a terminal exists, and get the message bit
225 *     and the access time
226 */
227term_chk(tty, msgsokP, atimeP, showerror)
228	char *tty;
229	int *msgsokP, showerror;
230	time_t *atimeP;
231{
232	struct stat s;
233	char path[MAXPATHLEN];
234
235	(void)sprintf(path, "/dev/%s", tty);
236	if (stat(path, &s) < 0) {
237		if (showerror)
238			(void)fprintf(stderr,
239			    "write: %s: %s\n", path, strerror(errno));
240		return(1);
241	}
242	*msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0;	/* group write bit */
243	*atimeP = s.st_atime;
244	return(0);
245}
246
247/*
248 * do_write - actually make the connection
249 */
250do_write(tty, mytty, myuid)
251	char *tty, *mytty;
252	uid_t myuid;
253{
254	register char *login, *nows;
255	register struct passwd *pwd;
256	time_t now, time();
257	char *getlogin(), path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
258	void done();
259
260	/* Determine our login name before the we reopen() stdout */
261	if ((login = getlogin()) == NULL)
262		if (pwd = getpwuid(myuid))
263			login = pwd->pw_name;
264		else
265			login = "???";
266
267	(void)sprintf(path, "/dev/%s", tty);
268	if ((freopen(path, "w", stdout)) == NULL) {
269		(void)fprintf(stderr, "write: %s: %s\n", path, strerror(errno));
270		exit(1);
271	}
272
273	(void)signal(SIGINT, done);
274	(void)signal(SIGHUP, done);
275
276	/* print greeting */
277	if (gethostname(host, sizeof(host)) < 0)
278		(void)strcpy(host, "???");
279	now = time((time_t *)NULL);
280	nows = ctime(&now);
281	nows[16] = '\0';
282	(void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
283	    login, host, mytty, nows + 11);
284
285	while (fgets(line, sizeof(line), stdin) != NULL)
286		wr_fputs(line);
287}
288
289/*
290 * done - cleanup and exit
291 */
292void
293done()
294{
295	(void)printf("EOF\r\n");
296	exit(0);
297}
298
299/*
300 * wr_fputs - like fputs(), but makes control characters visible and
301 *     turns \n into \r\n
302 */
303wr_fputs(s)
304	register unsigned char *s;
305{
306
307#define	PUTC(c)	if (putchar(c) == EOF) goto err;
308
309	for (; *s != '\0'; ++s) {
310		if (*s == '\n') {
311			PUTC('\r');
312		} else if (!isprint(*s) && !isspace(*s) && *s != '\007') {
313			if (*s & 0x80) {
314				*s &= ~0x80;
315				PUTC('M');
316				PUTC('-');
317			}
318			if (iscntrl(*s)) {
319				*s ^= 0x40;
320				PUTC('^');
321			}
322		}
323		PUTC(*s);
324	}
325	return;
326
327err:	(void)fprintf(stderr, "write: %s\n", strerror(errno));
328	exit(1);
329#undef PUTC
330}
331