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