Deleted Added
full compact
write.c (200160) write.c (202200)
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
38static const char copyright[] =
39"@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
41#endif
42
43#if 0
44#ifndef lint
45static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
46#endif
47#endif
48
49#include <sys/cdefs.h>
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
38static const char copyright[] =
39"@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
41#endif
42
43#if 0
44#ifndef lint
45static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
46#endif
47#endif
48
49#include <sys/cdefs.h>
50__FBSDID("$FreeBSD: head/usr.bin/write/write.c 200160 2009-12-05 20:22:26Z ed $");
50__FBSDID("$FreeBSD: head/usr.bin/write/write.c 202200 2010-01-13 18:09:54Z ed $");
51
52#include <sys/param.h>
53#include <sys/signal.h>
54#include <sys/stat.h>
55#include <sys/file.h>
56#include <sys/time.h>
57#include <ctype.h>
58#include <err.h>
59#include <locale.h>
60#include <paths.h>
61#include <pwd.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
51
52#include <sys/param.h>
53#include <sys/signal.h>
54#include <sys/stat.h>
55#include <sys/file.h>
56#include <sys/time.h>
57#include <ctype.h>
58#include <err.h>
59#include <locale.h>
60#include <paths.h>
61#include <pwd.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#define _ULOG_POSIX_NAMES
66#include <ulog.h>
67#include <unistd.h>
65#include <unistd.h>
66#include <utmpx.h>
68
69void done(int);
70void do_write(char *, char *, uid_t);
71static void usage(void);
72int term_chk(char *, int *, time_t *, int);
73void wr_fputs(unsigned char *s);
74void search_utmp(char *, char *, char *, uid_t);
75int utmp_chk(char *, char *);
76
77int
78main(int argc, char **argv)
79{
80 time_t atime;
81 uid_t myuid;
82 int msgsok, myttyfd;
83 char tty[MAXPATHLEN], *mytty;
84
85 (void)setlocale(LC_CTYPE, "");
86
87 while (getopt(argc, argv, "") != -1)
88 usage();
89 argc -= optind;
90 argv += optind;
91
92 /* check that sender has write enabled */
93 if (isatty(fileno(stdin)))
94 myttyfd = fileno(stdin);
95 else if (isatty(fileno(stdout)))
96 myttyfd = fileno(stdout);
97 else if (isatty(fileno(stderr)))
98 myttyfd = fileno(stderr);
99 else
100 errx(1, "can't find your tty");
101 if (!(mytty = ttyname(myttyfd)))
102 errx(1, "can't find your tty's name");
103 if (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV)))
104 mytty += strlen(_PATH_DEV);
105 if (term_chk(mytty, &msgsok, &atime, 1))
106 exit(1);
107 if (!msgsok)
108 errx(1, "you have write permission turned off");
109
110 myuid = getuid();
111
112 /* check args */
113 switch (argc) {
114 case 1:
115 search_utmp(argv[0], tty, mytty, myuid);
116 do_write(tty, mytty, myuid);
117 break;
118 case 2:
119 if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV)))
120 argv[1] += strlen(_PATH_DEV);
121 if (utmp_chk(argv[0], argv[1]))
122 errx(1, "%s is not logged in on %s", argv[0], argv[1]);
123 if (term_chk(argv[1], &msgsok, &atime, 1))
124 exit(1);
125 if (myuid && !msgsok)
126 errx(1, "%s has messages disabled on %s", argv[0], argv[1]);
127 do_write(argv[1], mytty, myuid);
128 break;
129 default:
130 usage();
131 }
132 done(0);
133 return (0);
134}
135
136static void
137usage(void)
138{
139 (void)fprintf(stderr, "usage: write user [tty]\n");
140 exit(1);
141}
142
143/*
144 * utmp_chk - checks that the given user is actually logged in on
145 * the given tty
146 */
147int
148utmp_chk(char *user, char *tty)
149{
150 struct utmpx lu, *u;
151
152 strncpy(lu.ut_line, tty, sizeof lu.ut_line);
153 setutxent();
154 while ((u = getutxline(&lu)) != NULL)
155 if (u->ut_type == USER_PROCESS &&
156 strcmp(user, u->ut_user) == 0) {
157 endutxent();
158 return(0);
159 }
160 endutxent();
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 */
175void
176search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
177{
178 struct utmpx *u;
179 time_t bestatime, atime;
180 int nloggedttys, nttys, msgsok, user_is_me;
181
182 nloggedttys = nttys = 0;
183 bestatime = 0;
184 user_is_me = 0;
185
186 setutxent();
187 while ((u = getutxent()) != NULL)
188 if (u->ut_type == USER_PROCESS &&
189 strcmp(user, u->ut_user) == 0) {
190 ++nloggedttys;
191 if (term_chk(u->ut_line, &msgsok, &atime, 0))
192 continue; /* bad term? skip */
193 if (myuid && !msgsok)
194 continue; /* skip ttys with msgs off */
195 if (strcmp(u->ut_line, mytty) == 0) {
196 user_is_me = 1;
197 continue; /* don't write to yourself */
198 }
199 ++nttys;
200 if (atime > bestatime) {
201 bestatime = atime;
202 (void)strlcpy(tty, u->ut_line, MAXPATHLEN);
203 }
204 }
205 endutxent();
206
207 if (nloggedttys == 0)
208 errx(1, "%s is not logged in", user);
209 if (nttys == 0) {
210 if (user_is_me) { /* ok, so write to yourself! */
211 (void)strlcpy(tty, mytty, MAXPATHLEN);
212 return;
213 }
214 errx(1, "%s has messages disabled", user);
215 } else if (nttys > 1) {
216 warnx("%s is logged in more than once; writing to %s", user, tty);
217 }
218}
219
220/*
221 * term_chk - check that a terminal exists, and get the message bit
222 * and the access time
223 */
224int
225term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
226{
227 struct stat s;
228 char path[MAXPATHLEN];
229
230 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
231 if (stat(path, &s) < 0) {
232 if (showerror)
233 warn("%s", path);
234 return(1);
235 }
236 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */
237 *atimeP = s.st_atime;
238 return(0);
239}
240
241/*
242 * do_write - actually make the connection
243 */
244void
245do_write(char *tty, char *mytty, uid_t myuid)
246{
247 const char *login;
248 char *nows;
249 struct passwd *pwd;
250 time_t now;
251 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
252
253 /* Determine our login name before we reopen() stdout */
254 if ((login = getlogin()) == NULL) {
255 if ((pwd = getpwuid(myuid)))
256 login = pwd->pw_name;
257 else
258 login = "???";
259 }
260
261 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
262 if ((freopen(path, "w", stdout)) == NULL)
263 err(1, "%s", path);
264
265 (void)signal(SIGINT, done);
266 (void)signal(SIGHUP, done);
267
268 /* print greeting */
269 if (gethostname(host, sizeof(host)) < 0)
270 (void)strcpy(host, "???");
271 now = time((time_t *)NULL);
272 nows = ctime(&now);
273 nows[16] = '\0';
274 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
275 login, host, mytty, nows + 11);
276
277 while (fgets(line, sizeof(line), stdin) != NULL)
278 wr_fputs(line);
279}
280
281/*
282 * done - cleanup and exit
283 */
284void
285done(int n __unused)
286{
287 (void)printf("EOF\r\n");
288 exit(0);
289}
290
291/*
292 * wr_fputs - like fputs(), but makes control characters visible and
293 * turns \n into \r\n
294 */
295void
296wr_fputs(unsigned char *s)
297{
298
299#define PUTC(c) if (putchar(c) == EOF) err(1, NULL);
300
301 for (; *s != '\0'; ++s) {
302 if (*s == '\n') {
303 PUTC('\r');
304 } else if (((*s & 0x80) && *s < 0xA0) ||
305 /* disable upper controls */
306 (!isprint(*s) && !isspace(*s) &&
307 *s != '\a' && *s != '\b')
308 ) {
309 if (*s & 0x80) {
310 *s &= ~0x80;
311 PUTC('M');
312 PUTC('-');
313 }
314 if (iscntrl(*s)) {
315 *s ^= 0x40;
316 PUTC('^');
317 }
318 }
319 PUTC(*s);
320 }
321 return;
322#undef PUTC
323}
67
68void done(int);
69void do_write(char *, char *, uid_t);
70static void usage(void);
71int term_chk(char *, int *, time_t *, int);
72void wr_fputs(unsigned char *s);
73void search_utmp(char *, char *, char *, uid_t);
74int utmp_chk(char *, char *);
75
76int
77main(int argc, char **argv)
78{
79 time_t atime;
80 uid_t myuid;
81 int msgsok, myttyfd;
82 char tty[MAXPATHLEN], *mytty;
83
84 (void)setlocale(LC_CTYPE, "");
85
86 while (getopt(argc, argv, "") != -1)
87 usage();
88 argc -= optind;
89 argv += optind;
90
91 /* check that sender has write enabled */
92 if (isatty(fileno(stdin)))
93 myttyfd = fileno(stdin);
94 else if (isatty(fileno(stdout)))
95 myttyfd = fileno(stdout);
96 else if (isatty(fileno(stderr)))
97 myttyfd = fileno(stderr);
98 else
99 errx(1, "can't find your tty");
100 if (!(mytty = ttyname(myttyfd)))
101 errx(1, "can't find your tty's name");
102 if (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV)))
103 mytty += strlen(_PATH_DEV);
104 if (term_chk(mytty, &msgsok, &atime, 1))
105 exit(1);
106 if (!msgsok)
107 errx(1, "you have write permission turned off");
108
109 myuid = getuid();
110
111 /* check args */
112 switch (argc) {
113 case 1:
114 search_utmp(argv[0], tty, mytty, myuid);
115 do_write(tty, mytty, myuid);
116 break;
117 case 2:
118 if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV)))
119 argv[1] += strlen(_PATH_DEV);
120 if (utmp_chk(argv[0], argv[1]))
121 errx(1, "%s is not logged in on %s", argv[0], argv[1]);
122 if (term_chk(argv[1], &msgsok, &atime, 1))
123 exit(1);
124 if (myuid && !msgsok)
125 errx(1, "%s has messages disabled on %s", argv[0], argv[1]);
126 do_write(argv[1], mytty, myuid);
127 break;
128 default:
129 usage();
130 }
131 done(0);
132 return (0);
133}
134
135static void
136usage(void)
137{
138 (void)fprintf(stderr, "usage: write user [tty]\n");
139 exit(1);
140}
141
142/*
143 * utmp_chk - checks that the given user is actually logged in on
144 * the given tty
145 */
146int
147utmp_chk(char *user, char *tty)
148{
149 struct utmpx lu, *u;
150
151 strncpy(lu.ut_line, tty, sizeof lu.ut_line);
152 setutxent();
153 while ((u = getutxline(&lu)) != NULL)
154 if (u->ut_type == USER_PROCESS &&
155 strcmp(user, u->ut_user) == 0) {
156 endutxent();
157 return(0);
158 }
159 endutxent();
160 return(1);
161}
162
163/*
164 * search_utmp - search utmp for the "best" terminal to write to
165 *
166 * Ignores terminals with messages disabled, and of the rest, returns
167 * the one with the most recent access time. Returns as value the number
168 * of the user's terminals with messages enabled, or -1 if the user is
169 * not logged in at all.
170 *
171 * Special case for writing to yourself - ignore the terminal you're
172 * writing from, unless that's the only terminal with messages enabled.
173 */
174void
175search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
176{
177 struct utmpx *u;
178 time_t bestatime, atime;
179 int nloggedttys, nttys, msgsok, user_is_me;
180
181 nloggedttys = nttys = 0;
182 bestatime = 0;
183 user_is_me = 0;
184
185 setutxent();
186 while ((u = getutxent()) != NULL)
187 if (u->ut_type == USER_PROCESS &&
188 strcmp(user, u->ut_user) == 0) {
189 ++nloggedttys;
190 if (term_chk(u->ut_line, &msgsok, &atime, 0))
191 continue; /* bad term? skip */
192 if (myuid && !msgsok)
193 continue; /* skip ttys with msgs off */
194 if (strcmp(u->ut_line, 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)strlcpy(tty, u->ut_line, MAXPATHLEN);
202 }
203 }
204 endutxent();
205
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)strlcpy(tty, mytty, MAXPATHLEN);
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", user, tty);
216 }
217}
218
219/*
220 * term_chk - check that a terminal exists, and get the message bit
221 * and the access time
222 */
223int
224term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
225{
226 struct stat s;
227 char path[MAXPATHLEN];
228
229 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
230 if (stat(path, &s) < 0) {
231 if (showerror)
232 warn("%s", path);
233 return(1);
234 }
235 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */
236 *atimeP = s.st_atime;
237 return(0);
238}
239
240/*
241 * do_write - actually make the connection
242 */
243void
244do_write(char *tty, char *mytty, uid_t myuid)
245{
246 const char *login;
247 char *nows;
248 struct passwd *pwd;
249 time_t now;
250 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
251
252 /* Determine our login name before we reopen() stdout */
253 if ((login = getlogin()) == NULL) {
254 if ((pwd = getpwuid(myuid)))
255 login = pwd->pw_name;
256 else
257 login = "???";
258 }
259
260 (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
261 if ((freopen(path, "w", stdout)) == NULL)
262 err(1, "%s", path);
263
264 (void)signal(SIGINT, done);
265 (void)signal(SIGHUP, done);
266
267 /* print greeting */
268 if (gethostname(host, sizeof(host)) < 0)
269 (void)strcpy(host, "???");
270 now = time((time_t *)NULL);
271 nows = ctime(&now);
272 nows[16] = '\0';
273 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
274 login, host, mytty, nows + 11);
275
276 while (fgets(line, sizeof(line), stdin) != NULL)
277 wr_fputs(line);
278}
279
280/*
281 * done - cleanup and exit
282 */
283void
284done(int n __unused)
285{
286 (void)printf("EOF\r\n");
287 exit(0);
288}
289
290/*
291 * wr_fputs - like fputs(), but makes control characters visible and
292 * turns \n into \r\n
293 */
294void
295wr_fputs(unsigned char *s)
296{
297
298#define PUTC(c) if (putchar(c) == EOF) err(1, NULL);
299
300 for (; *s != '\0'; ++s) {
301 if (*s == '\n') {
302 PUTC('\r');
303 } else if (((*s & 0x80) && *s < 0xA0) ||
304 /* disable upper controls */
305 (!isprint(*s) && !isspace(*s) &&
306 *s != '\a' && *s != '\b')
307 ) {
308 if (*s & 0x80) {
309 *s &= ~0x80;
310 PUTC('M');
311 PUTC('-');
312 }
313 if (iscntrl(*s)) {
314 *s ^= 0x40;
315 PUTC('^');
316 }
317 }
318 PUTC(*s);
319 }
320 return;
321#undef PUTC
322}