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