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