xref: /freebsd/usr.bin/write/write.c (revision 9ee40678bbdcedc6a3ac1e311abe740018911cf1)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 static char sccsid[] = "@(#)write.c	8.1 (Berkeley) 6/6/93";
45 #endif /* not lint */
46 
47 #include <sys/param.h>
48 #include <sys/signal.h>
49 #include <sys/stat.h>
50 #include <sys/file.h>
51 #include <sys/time.h>
52 #include <utmp.h>
53 #include <ctype.h>
54 #include <paths.h>
55 #include <pwd.h>
56 #include <stdio.h>
57 #include <string.h>
58 
59 extern int errno;
60 
61 main(argc, argv)
62 	int argc;
63 	char **argv;
64 {
65 	register char *cp;
66 	time_t atime;
67 	uid_t myuid;
68 	int msgsok, myttyfd;
69 	char tty[MAXPATHLEN], *mytty, *ttyname();
70 	void done();
71 
72 	/* check that sender has write enabled */
73 	if (isatty(fileno(stdin)))
74 		myttyfd = fileno(stdin);
75 	else if (isatty(fileno(stdout)))
76 		myttyfd = fileno(stdout);
77 	else if (isatty(fileno(stderr)))
78 		myttyfd = fileno(stderr);
79 	else {
80 		(void)fprintf(stderr, "write: can't find your tty\n");
81 		exit(1);
82 	}
83 	if (!(mytty = ttyname(myttyfd))) {
84 		(void)fprintf(stderr, "write: can't find your tty's name\n");
85 		exit(1);
86 	}
87 	if (cp = rindex(mytty, '/'))
88 		mytty = cp + 1;
89 	if (term_chk(mytty, &msgsok, &atime, 1))
90 		exit(1);
91 	if (!msgsok) {
92 		(void)fprintf(stderr,
93 		    "write: you have write permission turned off.\n");
94 		exit(1);
95 	}
96 
97 	myuid = getuid();
98 
99 	/* check args */
100 	switch (argc) {
101 	case 2:
102 		search_utmp(argv[1], tty, mytty, myuid);
103 		do_write(tty, mytty, myuid);
104 		break;
105 	case 3:
106 		if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV)))
107 			argv[2] += 5;
108 		if (utmp_chk(argv[1], argv[2])) {
109 			(void)fprintf(stderr,
110 			    "write: %s is not logged in on %s.\n",
111 			    argv[1], argv[2]);
112 			exit(1);
113 		}
114 		if (term_chk(argv[2], &msgsok, &atime, 1))
115 			exit(1);
116 		if (myuid && !msgsok) {
117 			(void)fprintf(stderr,
118 			    "write: %s has messages disabled on %s\n",
119 			    argv[1], argv[2]);
120 			exit(1);
121 		}
122 		do_write(argv[2], mytty, myuid);
123 		break;
124 	default:
125 		(void)fprintf(stderr, "usage: write user [tty]\n");
126 		exit(1);
127 	}
128 	done();
129 	/* NOTREACHED */
130 }
131 
132 /*
133  * utmp_chk - checks that the given user is actually logged in on
134  *     the given tty
135  */
136 utmp_chk(user, tty)
137 	char *user, *tty;
138 {
139 	struct utmp u;
140 	int ufd;
141 
142 	if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
143 		return(0);	/* ignore error, shouldn't happen anyway */
144 
145 	while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
146 		if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
147 		    strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
148 			(void)close(ufd);
149 			return(0);
150 		}
151 
152 	(void)close(ufd);
153 	return(1);
154 }
155 
156 /*
157  * search_utmp - search utmp for the "best" terminal to write to
158  *
159  * Ignores terminals with messages disabled, and of the rest, returns
160  * the one with the most recent access time.  Returns as value the number
161  * of the user's terminals with messages enabled, or -1 if the user is
162  * not logged in at all.
163  *
164  * Special case for writing to yourself - ignore the terminal you're
165  * writing from, unless that's the only terminal with messages enabled.
166  */
167 search_utmp(user, tty, mytty, myuid)
168 	char *user, *tty, *mytty;
169 	uid_t myuid;
170 {
171 	struct utmp u;
172 	time_t bestatime, atime;
173 	int ufd, nloggedttys, nttys, msgsok, user_is_me;
174 	char atty[UT_LINESIZE + 1];
175 
176 	if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) {
177 		perror("utmp");
178 		exit(1);
179 	}
180 
181 	nloggedttys = nttys = 0;
182 	bestatime = 0;
183 	user_is_me = 0;
184 	while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
185 		if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
186 			++nloggedttys;
187 			(void)strncpy(atty, u.ut_line, UT_LINESIZE);
188 			atty[UT_LINESIZE] = '\0';
189 			if (term_chk(atty, &msgsok, &atime, 0))
190 				continue;	/* bad term? skip */
191 			if (myuid && !msgsok)
192 				continue;	/* skip ttys with msgs off */
193 			if (strcmp(atty, mytty) == 0) {
194 				user_is_me = 1;
195 				continue;	/* don't write to yourself */
196 			}
197 			++nttys;
198 			if (atime > bestatime) {
199 				bestatime = atime;
200 				(void)strcpy(tty, atty);
201 			}
202 		}
203 
204 	(void)close(ufd);
205 	if (nloggedttys == 0) {
206 		(void)fprintf(stderr, "write: %s is not logged in\n", user);
207 		exit(1);
208 	}
209 	if (nttys == 0) {
210 		if (user_is_me) {		/* ok, so write to yourself! */
211 			(void)strcpy(tty, mytty);
212 			return;
213 		}
214 		(void)fprintf(stderr,
215 		    "write: %s has messages disabled\n", user);
216 		exit(1);
217 	} else if (nttys > 1) {
218 		(void)fprintf(stderr,
219 		    "write: %s is logged in more than once; writing to %s\n",
220 		    user, tty);
221 	}
222 }
223 
224 /*
225  * term_chk - check that a terminal exists, and get the message bit
226  *     and the access time
227  */
228 term_chk(tty, msgsokP, atimeP, showerror)
229 	char *tty;
230 	int *msgsokP, showerror;
231 	time_t *atimeP;
232 {
233 	struct stat s;
234 	char path[MAXPATHLEN];
235 
236 	(void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
237 	if (stat(path, &s) < 0) {
238 		if (showerror)
239 			(void)fprintf(stderr,
240 			    "write: %s: %s\n", path, strerror(errno));
241 		return(1);
242 	}
243 	*msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0;	/* group write bit */
244 	*atimeP = s.st_atime;
245 	return(0);
246 }
247 
248 /*
249  * do_write - actually make the connection
250  */
251 do_write(tty, mytty, myuid)
252 	char *tty, *mytty;
253 	uid_t myuid;
254 {
255 	register char *login, *nows;
256 	register struct passwd *pwd;
257 	time_t now, time();
258 	char *getlogin(), path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
259 	void done();
260 
261 	/* Determine our login name before the we reopen() stdout */
262 	if ((login = getlogin()) == NULL)
263 		if (pwd = getpwuid(myuid))
264 			login = pwd->pw_name;
265 		else
266 			login = "???";
267 
268 	(void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
269 	if ((freopen(path, "w", stdout)) == NULL) {
270 		(void)fprintf(stderr, "write: %s: %s\n", path, strerror(errno));
271 		exit(1);
272 	}
273 
274 	(void)signal(SIGINT, done);
275 	(void)signal(SIGHUP, done);
276 
277 	/* print greeting */
278 	if (gethostname(host, sizeof(host)) < 0)
279 		(void)strcpy(host, "???");
280 	now = time((time_t *)NULL);
281 	nows = ctime(&now);
282 	nows[16] = '\0';
283 	(void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
284 	    login, host, mytty, nows + 11);
285 
286 	while (fgets(line, sizeof(line), stdin) != NULL)
287 		wr_fputs(line);
288 }
289 
290 /*
291  * done - cleanup and exit
292  */
293 void
294 done()
295 {
296 	(void)printf("EOF\r\n");
297 	exit(0);
298 }
299 
300 /*
301  * wr_fputs - like fputs(), but makes control characters visible and
302  *     turns \n into \r\n
303  */
304 wr_fputs(s)
305 	register unsigned char *s;
306 {
307 
308 #define	PUTC(c)	if (putchar(c) == EOF) goto err;
309 
310 	for (; *s != '\0'; ++s) {
311 		if (*s == '\n') {
312 			PUTC('\r');
313 		} else if (!isprint(*s) && !isspace(*s) && *s != '\007') {
314 			if (*s & 0x80) {
315 				*s &= ~0x80;
316 				PUTC('M');
317 				PUTC('-');
318 			}
319 			if (iscntrl(*s)) {
320 				*s ^= 0x40;
321 				PUTC('^');
322 			}
323 		}
324 		PUTC(*s);
325 	}
326 	return;
327 
328 err:	(void)fprintf(stderr, "write: %s\n", strerror(errno));
329 	exit(1);
330 #undef PUTC
331 }
332