xref: /dragonfly/usr.bin/write/write.c (revision 25606b42)
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
33  * @(#)write.c	8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.bin/write/write.c,v 1.12 1999/08/28 01:07:48 peter Exp $
35  */
36 
37 #include <sys/param.h>
38 #include <sys/stat.h>
39 #include <sys/file.h>
40 
41 #include <ctype.h>
42 #include <err.h>
43 #include <locale.h>
44 #include <paths.h>
45 #include <pwd.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <signal.h>
50 #include <time.h>
51 #include <unistd.h>
52 #include "utmpentry.h"
53 
54 static void	done(int);
55 static void	do_write(const char *, const char *, uid_t, int *);
56 static void	usage(void);
57 static int	term_chk(const char *, int *, time_t *, int);
58 static void	wr_fputs(unsigned char *s);
59 static void	search_utmpx(const char *, char *, size_t, const char *, uid_t);
60 static int	utmpx_chk(const char *, const char *);
61 
62 int
main(int argc,char ** argv)63 main(int argc, char **argv)
64 {
65 	time_t atime;
66 	uid_t myuid;
67 	int msgsok, mymsgok, myttyfd;
68 	char tty[MAXPATHLEN], *mytty;
69 
70 	setlocale(LC_CTYPE, "");
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 		errx(1, "can't find your tty");
81 	if (!(mytty = ttyname(myttyfd)))
82 		errx(1, "can't find your tty's name");
83 	if (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV)))
84 		mytty += strlen(_PATH_DEV);
85 	if (term_chk(mytty, &mymsgok, &atime, 1))
86 		exit(1);
87 	if (!mymsgok)
88 		warnx("you have write permission turned off (man mesg)");
89 
90 	myuid = getuid();
91 
92 	/* check args */
93 	switch (argc) {
94 	case 2:
95 		search_utmpx(argv[1], tty, sizeof tty, mytty, myuid);
96 		do_write(tty, mytty, myuid, &mymsgok);
97 		break;
98 	case 3:
99 		if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV)))
100 			argv[2] += strlen(_PATH_DEV);
101 		if (utmpx_chk(argv[1], argv[2]))
102 			errx(1, "%s is not logged in on %s", argv[1], argv[2]);
103 		if (term_chk(argv[2], &msgsok, &atime, 1))
104 			exit(1);
105 		if (myuid && !msgsok)
106 			errx(1, "%s has messages disabled on %s", argv[1], argv[2]);
107 		do_write(argv[2], mytty, myuid, &mymsgok);
108 		break;
109 	default:
110 		usage();
111 	}
112 	done(0);
113 	return (0);
114 }
115 
116 static void
usage(void)117 usage(void)
118 {
119 	fprintf(stderr, "usage: write user [tty]\n");
120 	exit(1);
121 }
122 
123 /*
124  * utmpx_chk - checks that the given user is actually logged in on
125  *     the given tty
126  */
127 static int
utmpx_chk(const char * user,const char * tty)128 utmpx_chk(const char *user, const char *tty)
129 {
130 	struct utmpentry *ep;
131 
132 	getutentries(NULL, &ep);
133 
134 	for (; ep; ep = ep->next) {
135 		if (strcmp(user, ep->name) == 0 &&
136 		    strcmp(tty, ep->line) == 0) {
137 			return(0);
138 		}
139 	}
140 
141 	return(1);
142 }
143 
144 /*
145  * search_utmpx - search utmpx for the "best" terminal to write to
146  *
147  * Ignores terminals with messages disabled, and of the rest, returns
148  * the one with the most recent access time.  Returns as value the number
149  * of the user's terminals with messages enabled, or -1 if the user is
150  * not logged in at all.
151  *
152  * Special case for writing to yourself - ignore the terminal you're
153  * writing from, unless that's the only terminal with messages enabled.
154  */
155 static void
search_utmpx(const char * user,char * tty,size_t ttyl,const char * mytty,uid_t myuid)156 search_utmpx(const char *user, char *tty, size_t ttyl, const char *mytty, uid_t myuid)
157 {
158 	struct utmpentry *ep;
159 	time_t bestatime, atime;
160 	int nloggedttys, nttys, msgsok, user_is_me;
161 
162 	getutentries(NULL, &ep);
163 
164 	nloggedttys = nttys = 0;
165 	bestatime = 0;
166 	user_is_me = 0;
167 	for (; ep; ep = ep->next)
168 		if (strcmp(user, ep->name) == 0) {
169 			++nloggedttys;
170 			if (term_chk(ep->line, &msgsok, &atime, 0))
171 				continue;	/* bad term? skip */
172 			if (myuid && !msgsok)
173 				continue;	/* skip ttys with msgs off */
174 			if (strcmp(ep->line, mytty) == 0) {
175 				user_is_me = 1;
176 				continue;	/* don't write to yourself */
177 			}
178 			++nttys;
179 			if (atime > bestatime) {
180 				bestatime = atime;
181 				strlcpy(tty, ep->line, ttyl);
182 			}
183 		}
184 
185 	if (nloggedttys == 0)
186 		errx(1, "%s is not logged in", user);
187 	if (nttys == 0) {
188 		if (user_is_me) {		/* ok, so write to yourself! */
189 			strlcpy(tty, mytty, ttyl);
190 			return;
191 		}
192 		errx(1, "%s has messages disabled", user);
193 	} else if (nttys > 1) {
194 		warnx("%s is logged in more than once; writing to %s", user, tty);
195 	}
196 }
197 
198 /*
199  * term_chk - check that a terminal exists, and get the message bit
200  *     and the access time
201  */
202 static int
term_chk(const char * tty,int * msgsokP,time_t * atimeP,int showerror)203 term_chk(const char *tty, int *msgsokP, time_t *atimeP, int showerror)
204 {
205 	struct stat s;
206 	char path[MAXPATHLEN];
207 
208 	snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
209 	if (stat(path, &s) < 0) {
210 		if (showerror)
211 			warn("%s", path);
212 		return(1);
213 	}
214 	*msgsokP = (s.st_mode & S_IWGRP) != 0;	/* group write bit */
215 	*atimeP = s.st_atime;
216 	return(0);
217 }
218 
219 /*
220  * do_write - actually make the connection
221  */
222 static void
do_write(const char * tty,const char * mytty,uid_t myuid,int * mymsgok)223 do_write(const char *tty, const char *mytty, uid_t myuid, int *mymsgok)
224 {
225 	const char *login;
226 	char *nows;
227 	struct passwd *pwd;
228 	time_t now;
229 	char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
230 
231 	/* Determine our login name before we reopen() stdout */
232 	if ((login = getlogin()) == NULL) {
233 		if ((pwd = getpwuid(myuid)))
234 			login = pwd->pw_name;
235 		else
236 			login = "???";
237 	}
238 
239 	snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
240 	if ((freopen(path, "w", stdout)) == NULL)
241 		err(1, "%s", path);
242 
243 	signal(SIGINT, done);
244 	signal(SIGHUP, done);
245 
246 	/* print greeting */
247 	if (gethostname(host, sizeof(host)) < 0)
248 		strlcpy(host, "???", sizeof host);
249 	now = time(NULL);
250 	nows = ctime(&now);
251 	nows[16] = '\0';
252 	printf("\r\n\007\007\007Message from %s@%s on %s at %s (%s %s replies)...\r\n",
253 	    login, host, mytty, nows + 11, login, *mymsgok == 0 ? "does not accept" : "accepts");
254 
255 	while (fgets(line, sizeof(line), stdin) != NULL)
256 		wr_fputs(line);
257 }
258 
259 /*
260  * done - cleanup and exit
261  */
262 static void
done(__unused int n)263 done(__unused int n)
264 {
265 	printf("EOF\r\n");
266 	exit(0);
267 }
268 
269 /*
270  * wr_fputs - like fputs(), but makes control characters visible and
271  *     turns \n into \r\n
272  */
273 static void
wr_fputs(unsigned char * s)274 wr_fputs(unsigned char *s)
275 {
276 
277 #define	PUTC(c)	if (putchar(c) == EOF) err(1, NULL);
278 
279 	for (; *s != '\0'; ++s) {
280 		if (*s == '\n') {
281 			PUTC('\r');
282 		} else if (((*s & 0x80) && *s < 0xA0) ||
283 			   /* disable upper controls */
284 			   (!isprint(*s) && !isspace(*s) &&
285 			    *s != '\a' && *s != '\b')
286 			  ) {
287 			if (*s & 0x80) {
288 				*s &= ~0x80;
289 				PUTC('M');
290 				PUTC('-');
291 			}
292 			if (iscntrl(*s)) {
293 				*s ^= 0x40;
294 				PUTC('^');
295 			}
296 		}
297 		PUTC(*s);
298 	}
299 	return;
300 #undef PUTC
301 }
302