write.c revision 1.1.1.3 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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
41 #endif /* not lint */
42
43 #ifndef lint
44 static char sccsid[] = "@(#)write.c 8.2 (Berkeley) 4/27/95";
45 #endif /* not lint */
46
47 #include <sys/param.h>
48 #include <sys/signal.h>
49 #include <sys/stat.h>
50 #include <sys/file.h>
51 #include <sys/time.h>
52
53 #include <err.h>
54 #include <utmp.h>
55 #include <ctype.h>
56 #include <pwd.h>
57 #include <stdio.h>
58 #include <string.h>
59
60 extern int errno;
61
62 main(argc, argv)
63 int argc;
64 char **argv;
65 {
66 register char *cp;
67 time_t atime;
68 uid_t myuid;
69 int msgsok, myttyfd;
70 char tty[MAXPATHLEN], *mytty, *ttyname();
71 void done();
72
73 /* check that sender has write enabled */
74 if (isatty(fileno(stdin)))
75 myttyfd = fileno(stdin);
76 else if (isatty(fileno(stdout)))
77 myttyfd = fileno(stdout);
78 else if (isatty(fileno(stderr)))
79 myttyfd = fileno(stderr);
80 else
81 errx(1, "can't find your tty");
82 if (!(mytty = ttyname(myttyfd)))
83 errx(1, "can't find your tty's name");
84 if (cp = strrchr(mytty, '/'))
85 mytty = cp + 1;
86 if (term_chk(mytty, &msgsok, &atime, 1))
87 exit(1);
88 if (!msgsok)
89 errx(1, "you have write permission turned off");
90
91 myuid = getuid();
92
93 /* check args */
94 switch (argc) {
95 case 2:
96 search_utmp(argv[1], tty, mytty, myuid);
97 do_write(tty, mytty, myuid);
98 break;
99 case 3:
100 if (!strncmp(argv[2], "/dev/", 5))
101 argv[2] += 5;
102 if (utmp_chk(argv[1], argv[2]))
103 errx(1, "%s is not logged in on %s",
104 argv[1], argv[2]);
105 if (term_chk(argv[2], &msgsok, &atime, 1))
106 exit(1);
107 if (myuid && !msgsok)
108 errx(1, "%s has messages disabled on %s",
109 argv[1], argv[2]);
110 do_write(argv[2], mytty, myuid);
111 break;
112 default:
113 (void)fprintf(stderr, "usage: write user [tty]\n");
114 exit(1);
115 }
116 done();
117 /* NOTREACHED */
118 }
119
120 /*
121 * utmp_chk - checks that the given user is actually logged in on
122 * the given tty
123 */
124 utmp_chk(user, tty)
125 char *user, *tty;
126 {
127 struct utmp u;
128 int ufd;
129
130 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
131 return(0); /* ignore error, shouldn't happen anyway */
132
133 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
134 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
135 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
136 (void)close(ufd);
137 return(0);
138 }
139
140 (void)close(ufd);
141 return(1);
142 }
143
144 /*
145 * search_utmp - search utmp 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 search_utmp(user, tty, mytty, myuid)
156 char *user, *tty, *mytty;
157 uid_t myuid;
158 {
159 struct utmp u;
160 time_t bestatime, atime;
161 int ufd, nloggedttys, nttys, msgsok, user_is_me;
162 char atty[UT_LINESIZE + 1];
163
164 if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
165 err(1, "%s", _PATH_UTMP);
166
167 nloggedttys = nttys = 0;
168 bestatime = 0;
169 user_is_me = 0;
170 while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
171 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
172 ++nloggedttys;
173 (void)strncpy(atty, u.ut_line, UT_LINESIZE);
174 atty[UT_LINESIZE] = '\0';
175 if (term_chk(atty, &msgsok, &atime, 0))
176 continue; /* bad term? skip */
177 if (myuid && !msgsok)
178 continue; /* skip ttys with msgs off */
179 if (strcmp(atty, mytty) == 0) {
180 user_is_me = 1;
181 continue; /* don't write to yourself */
182 }
183 ++nttys;
184 if (atime > bestatime) {
185 bestatime = atime;
186 (void)strcpy(tty, atty);
187 }
188 }
189
190 (void)close(ufd);
191 if (nloggedttys == 0)
192 errx(1, "%s is not logged in", user);
193 if (nttys == 0) {
194 if (user_is_me) { /* ok, so write to yourself! */
195 (void)strcpy(tty, mytty);
196 return;
197 }
198 errx(1, "%s has messages disabled", user);
199 } else if (nttys > 1)
200 warnx("%s is logged in more than once; writing to %s",
201 user, tty);
202 }
203
204 /*
205 * term_chk - check that a terminal exists, and get the message bit
206 * and the access time
207 */
208 term_chk(tty, msgsokP, atimeP, showerror)
209 char *tty;
210 int *msgsokP, showerror;
211 time_t *atimeP;
212 {
213 struct stat s;
214 char path[MAXPATHLEN];
215
216 (void)sprintf(path, "/dev/%s", tty);
217 if (stat(path, &s) < 0) {
218 if (showerror)
219 warn("%s", path);
220 return(1);
221 }
222 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */
223 *atimeP = s.st_atime;
224 return(0);
225 }
226
227 /*
228 * do_write - actually make the connection
229 */
230 do_write(tty, mytty, myuid)
231 char *tty, *mytty;
232 uid_t myuid;
233 {
234 register char *login, *nows;
235 register struct passwd *pwd;
236 time_t now, time();
237 char *getlogin(), path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
238 void done();
239
240 /* Determine our login name before the we reopen() stdout */
241 if ((login = getlogin()) == NULL)
242 if (pwd = getpwuid(myuid))
243 login = pwd->pw_name;
244 else
245 login = "???";
246
247 (void)sprintf(path, "/dev/%s", tty);
248 if ((freopen(path, "w", stdout)) == NULL)
249 err(1, "%s", path);
250
251 (void)signal(SIGINT, done);
252 (void)signal(SIGHUP, done);
253
254 /* print greeting */
255 if (gethostname(host, sizeof(host)) < 0)
256 (void)strcpy(host, "???");
257 now = time((time_t *)NULL);
258 nows = ctime(&now);
259 nows[16] = '\0';
260 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
261 login, host, mytty, nows + 11);
262
263 while (fgets(line, sizeof(line), stdin) != NULL)
264 wr_fputs(line);
265 }
266
267 /*
268 * done - cleanup and exit
269 */
270 void
271 done()
272 {
273 (void)printf("EOF\r\n");
274 exit(0);
275 }
276
277 /*
278 * wr_fputs - like fputs(), but makes control characters visible and
279 * turns \n into \r\n
280 */
281 wr_fputs(s)
282 register char *s;
283 {
284 register char c;
285
286 #define PUTC(c) if (putchar(c) == EOF) goto err;
287
288 for (; *s != '\0'; ++s) {
289 c = toascii(*s);
290 if (c == '\n') {
291 PUTC('\r');
292 PUTC('\n');
293 } else if (!isprint(c) && !isspace(c) && c != '\007') {
294 PUTC('^');
295 PUTC(c^0x40); /* DEL to ?, others to alpha */
296 } else
297 PUTC(c);
298 }
299 return;
300
301 err: err(1, NULL);
302 #undef PUTC
303 }
304