write.c revision 1.14 1 /* $NetBSD: write.c,v 1.14 1998/08/25 20:59:43 ross 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.14 1998/08/25 20:59:43 ross 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 <string.h>
59 #include <signal.h>
60 #include <time.h>
61 #include <fcntl.h>
62 #include <pwd.h>
63 #include <unistd.h>
64 #include <utmp.h>
65 #include <err.h>
66
67 void done __P((int));
68 void do_write __P((char *, char *, uid_t));
69 void wr_fputs __P((char *));
70 void search_utmp __P((char *, char *, char *, uid_t, int));
71 int term_chk __P((char *, int *, time_t *, int));
72 int utmp_chk __P((char *, char *));
73 int main __P((int, char **));
74
75 int
76 main(argc, argv)
77 int argc;
78 char **argv;
79 {
80 char *cp;
81 time_t atime;
82 uid_t myuid;
83 int msgsok, myttyfd;
84 char tty[MAXPATHLEN], *mytty;
85
86 /* check that sender has write enabled */
87 if (isatty(fileno(stdin)))
88 myttyfd = fileno(stdin);
89 else if (isatty(fileno(stdout)))
90 myttyfd = fileno(stdout);
91 else if (isatty(fileno(stderr)))
92 myttyfd = fileno(stderr);
93 else
94 errx(1, "can't find your tty");
95 if (!(mytty = ttyname(myttyfd)))
96 errx(1, "can't find your tty's name");
97 if ((cp = strrchr(mytty, '/')) != NULL)
98 mytty = cp + 1;
99 if (term_chk(mytty, &msgsok, &atime, 1))
100 exit(1);
101 if (!msgsok) {
102 (void)fprintf(stderr,
103 "warning: you have write permission turned off; "
104 "no reply possible\n");
105 }
106
107 myuid = getuid();
108
109 /* check args */
110 switch (argc) {
111 case 2:
112 search_utmp(argv[1], tty, mytty, myuid, sizeof tty);
113 do_write(tty, mytty, myuid);
114 break;
115 case 3:
116 if (!strncmp(argv[2], "/dev/", 5))
117 argv[2] += 5;
118 if (utmp_chk(argv[1], argv[2]))
119 errx(1, "%s is not logged in on %s",
120 argv[1], argv[2]);
121 if (term_chk(argv[2], &msgsok, &atime, 1))
122 exit(1);
123 if (myuid && !msgsok)
124 errx(1, "%s has messages disabled on %s",
125 argv[1], argv[2]);
126 do_write(argv[2], mytty, myuid);
127 break;
128 default:
129 (void)fprintf(stderr, "usage: write user [tty]\n");
130 exit(1);
131 }
132 done(0);
133 /* NOTREACHED */
134 #ifdef __GNUC__
135 return (0);
136 #endif
137 }
138
139 /*
140 * utmp_chk - checks that the given user is actually logged in on
141 * the given tty
142 */
143 int
144 utmp_chk(user, tty)
145 char *user, *tty;
146 {
147 struct utmp u;
148 int ufd;
149
150 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
151 return(0); /* ignore error, shouldn't happen anyway */
152
153 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
154 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
155 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
156 (void)close(ufd);
157 return(0);
158 }
159
160 (void)close(ufd);
161 return(1);
162 }
163
164 /*
165 * search_utmp - search utmp for the "best" terminal to write to
166 *
167 * Ignores terminals with messages disabled, and of the rest, returns
168 * the one with the most recent access time. Returns as value the number
169 * of the user's terminals with messages enabled, or -1 if the user is
170 * not logged in at all.
171 *
172 * Special case for writing to yourself - ignore the terminal you're
173 * writing from, unless that's the only terminal with messages enabled.
174 */
175 void
176 search_utmp(user, tty, mytty, myuid, ttylen)
177 char *user, *tty, *mytty;
178 uid_t myuid;
179 int ttylen;
180 {
181 struct utmp u;
182 time_t bestatime, atime;
183 int ufd, nloggedttys, nttys, msgsok, user_is_me;
184 char atty[UT_LINESIZE + 1];
185
186 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
187 err(1, "%s", _PATH_UTMP);
188
189 nloggedttys = nttys = 0;
190 bestatime = 0;
191 user_is_me = 0;
192 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
193 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
194 ++nloggedttys;
195 (void)strncpy(atty, u.ut_line, UT_LINESIZE);
196 atty[UT_LINESIZE] = '\0';
197 if (term_chk(atty, &msgsok, &atime, 0))
198 continue; /* bad term? skip */
199 if (myuid && !msgsok)
200 continue; /* skip ttys with msgs off */
201 if (strcmp(atty, mytty) == 0) {
202 user_is_me = 1;
203 continue; /* don't write to yourself */
204 }
205 ++nttys;
206 if (atime > bestatime) {
207 bestatime = atime;
208 (void)strncpy(tty, atty, ttylen - 1);
209 tty[ttylen - 1] = '\0';
210 }
211 }
212
213 (void)close(ufd);
214 if (nloggedttys == 0)
215 errx(1, "%s is not logged in", user);
216 if (nttys == 0) {
217 if (user_is_me) { /* ok, so write to yourself! */
218 (void)strncpy(tty, mytty, ttylen - 1);
219 tty[ttylen - 1] = '\0';
220 return;
221 }
222 errx(1, "%s has messages disabled", user);
223 } else if (nttys > 1)
224 warnx("%s is logged in more than once; writing to %s",
225 user, tty);
226 }
227
228 /*
229 * term_chk - check that a terminal exists, and get the message bit
230 * and the access time
231 */
232 int
233 term_chk(tty, msgsokP, atimeP, showerror)
234 char *tty;
235 int *msgsokP, showerror;
236 time_t *atimeP;
237 {
238 struct stat s;
239 char path[MAXPATHLEN];
240
241 (void)snprintf(path, sizeof path, "/dev/%s", tty);
242 if (stat(path, &s) < 0) {
243 if (showerror)
244 warn("%s", path);
245 return(1);
246 }
247 *msgsokP = (s.st_mode & S_IWGRP) != 0; /* group write bit */
248 *atimeP = s.st_atime;
249 return(0);
250 }
251
252 /*
253 * do_write - actually make the connection
254 */
255 void
256 do_write(tty, mytty, myuid)
257 char *tty, *mytty;
258 uid_t myuid;
259 {
260 const char *login;
261 char *nows;
262 struct passwd *pwd;
263 time_t now;
264 char path[MAXPATHLEN], host[MAXHOSTNAMELEN + 1], line[512];
265
266 /* Determine our login name before the we reopen() stdout */
267 if ((login = getlogin()) == NULL) {
268 if ((pwd = getpwuid(myuid)) != NULL)
269 login = pwd->pw_name;
270 else login = "???";
271 }
272 (void)snprintf(path, sizeof path, "/dev/%s", tty);
273 if ((freopen(path, "w", stdout)) == NULL)
274 err(1, "%s", path);
275
276 (void)signal(SIGINT, done);
277 (void)signal(SIGHUP, done);
278
279 /* print greeting */
280 if (gethostname(host, sizeof(host)) < 0)
281 (void)strncpy(host, "???", sizeof(host) - 1);
282 else
283 host[sizeof(host) - 1] = '\0';
284 now = time((time_t *)NULL);
285 nows = ctime(&now);
286 nows[16] = '\0';
287 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
288 login, host, mytty, nows + 11);
289
290 while (fgets(line, sizeof(line), stdin) != NULL)
291 wr_fputs(line);
292 }
293
294 /*
295 * done - cleanup and exit
296 */
297 void
298 done(dummy)
299 int dummy;
300 {
301
302 (void)printf("EOF\r\n");
303 exit(0);
304 }
305
306 /*
307 * wr_fputs - like fputs(), but makes control characters visible and
308 * turns \n into \r\n
309 */
310 void
311 wr_fputs(s)
312 char *s;
313 {
314 char c;
315
316 #define PUTC(c) if (putchar(c) == EOF) goto err;
317
318 for (; *s != '\0'; ++s) {
319 c = toascii(*s);
320 if (c == '\n') {
321 PUTC('\r');
322 PUTC('\n');
323 } else if (!isprint(c) && !isspace(c) && c != '\007') {
324 PUTC('^');
325 PUTC(c^0x40); /* DEL to ?, others to alpha */
326 } else
327 PUTC(c);
328 }
329 return;
330
331 err: err(1, "%s", "");
332 #undef PUTC
333 }
334