write.c revision 1.9 1 /* $NetBSD: write.c,v 1.9 1997/10/19 14:35:31 mrg 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.9 1997/10/19 14:35:31 mrg 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 register 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 }
210 }
211
212 (void)close(ufd);
213 if (nloggedttys == 0)
214 errx(1, "%s is not logged in", user);
215 if (nttys == 0) {
216 if (user_is_me) { /* ok, so write to yourself! */
217 (void)strncpy(tty, mytty, ttylen - 1);
218 return;
219 }
220 errx(1, "%s has messages disabled", user);
221 } else if (nttys > 1)
222 warnx("%s is logged in more than once; writing to %s",
223 user, tty);
224 }
225
226 /*
227 * term_chk - check that a terminal exists, and get the message bit
228 * and the access time
229 */
230 int
231 term_chk(tty, msgsokP, atimeP, showerror)
232 char *tty;
233 int *msgsokP, showerror;
234 time_t *atimeP;
235 {
236 struct stat s;
237 char path[MAXPATHLEN];
238
239 (void)snprintf(path, sizeof path, "/dev/%s", tty);
240 if (stat(path, &s) < 0) {
241 if (showerror)
242 warn("%s", path);
243 return(1);
244 }
245 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */
246 *atimeP = s.st_atime;
247 return(0);
248 }
249
250 /*
251 * do_write - actually make the connection
252 */
253 void
254 do_write(tty, mytty, myuid)
255 char *tty, *mytty;
256 uid_t myuid;
257 {
258 register char *login, *nows;
259 register struct passwd *pwd;
260 time_t now;
261 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
262
263 /* Determine our login name before the we reopen() stdout */
264 if ((login = getlogin()) == NULL)
265 if ((pwd = getpwuid(myuid)) != NULL)
266 login = pwd->pw_name;
267 else
268 login = "???";
269
270 (void)snprintf(path, sizeof path, "/dev/%s", tty);
271 if ((freopen(path, "w", stdout)) == NULL)
272 err(1, "%s", path);
273
274 (void)signal(SIGINT, done);
275 (void)signal(SIGHUP, done);
276
277 /* print greeting */
278 if (gethostname(host, sizeof(host)) < 0)
279 (void)strncpy(host, "???", sizeof(host) - 1);
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(dummy)
295 int dummy;
296 {
297 (void)printf("EOF\r\n");
298 exit(0);
299 }
300
301 /*
302 * wr_fputs - like fputs(), but makes control characters visible and
303 * turns \n into \r\n
304 */
305 void
306 wr_fputs(s)
307 register char *s;
308 {
309 register char c;
310
311 #define PUTC(c) if (putchar(c) == EOF) goto err;
312
313 for (; *s != '\0'; ++s) {
314 c = toascii(*s);
315 if (c == '\n') {
316 PUTC('\r');
317 PUTC('\n');
318 } else if (!isprint(c) && !isspace(c) && c != '\007') {
319 PUTC('^');
320 PUTC(c^0x40); /* DEL to ?, others to alpha */
321 } else
322 PUTC(c);
323 }
324 return;
325
326 err: err(1, "%s", "");
327 #undef PUTC
328 }
329