]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/write.c
Imported from util-linux-2.10s tarball.
[thirdparty/util-linux.git] / misc-utils / write.c
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 * Modified for Linux, Mon Mar 8 18:16:24 1993, faith@cs.unc.edu
37 * Wed Jun 22 21:41:56 1994, faith@cs.unc.edu:
38 * Added fix from Mike Grupenhoff (kashmir@umiacs.umd.edu)
39 * Mon Jul 1 17:01:39 MET DST 1996, janl@math.uio.no:
40 * - Added fix from David.Chapell@mail.trincoll.edu enabeling daemons
41 * to use write.
42 * - ANSIed it since I was working on it anyway.
43 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
44 * - added Native Language Support
45 *
46 */
47
48 #include <stdio.h>
49 #include <unistd.h>
50 #include <utmp.h>
51 #include <errno.h>
52 #include <ctype.h>
53 #include <time.h>
54 #include <pwd.h>
55 #include <string.h>
56 #include <stdlib.h>
57 #include <locale.h>
58 #include <signal.h>
59 #include <sys/param.h>
60 #include <sys/signal.h>
61 #include <sys/stat.h>
62 #include <sys/file.h>
63 #include <sys/time.h>
64 #include <paths.h>
65 #include "pathnames.h"
66 #include "carefulputc.h"
67 #include "nls.h"
68
69 void search_utmp(char *, char *, char *, uid_t);
70 void do_write(char *, char *, uid_t);
71 void wr_fputs(char *);
72 static void done(int);
73 int term_chk(char *, int *, time_t *, int);
74 int utmp_chk(char *, char *);
75
76 int
77 main(int argc, char **argv) {
78 time_t atime;
79 uid_t myuid;
80 int msgsok, myttyfd;
81 char tty[MAXPATHLEN], *mytty;
82
83 setlocale(LC_ALL, "");
84 bindtextdomain(PACKAGE, LOCALEDIR);
85 textdomain(PACKAGE);
86
87 /* check that sender has write enabled */
88 if (isatty(fileno(stdin)))
89 myttyfd = fileno(stdin);
90 else if (isatty(fileno(stdout)))
91 myttyfd = fileno(stdout);
92 else if (isatty(fileno(stderr)))
93 myttyfd = fileno(stderr);
94 else {
95 myttyfd = -1;
96 }
97 if (myttyfd != -1) {
98 if (!(mytty = ttyname(myttyfd))) {
99 (void)fprintf(stderr, _("write: can't find your tty's name\n"));
100 exit(1);
101 }
102 /* We may have /dev/ttyN but also /dev/pts/xx.
103 Below, term_chk() will put "/dev/" in front, so remove that part. */
104 if (!strncmp(mytty, "/dev/", 5))
105 mytty += 5;
106 if (term_chk(mytty, &msgsok, &atime, 1))
107 exit(1);
108 if (!msgsok) {
109 (void)fprintf(stderr,
110 _("write: you have write permission turned off.\n"));
111 exit(1);
112 }
113
114 } else {
115 mytty = "<no tty>";
116 }
117
118 myuid = getuid();
119
120 /* check args */
121 switch (argc) {
122 case 2:
123 search_utmp(argv[1], tty, mytty, myuid);
124 do_write(tty, mytty, myuid);
125 break;
126 case 3:
127 if (!strncmp(argv[2], "/dev/", 5))
128 argv[2] += 5;
129 if (utmp_chk(argv[1], argv[2])) {
130 (void)fprintf(stderr,
131 _("write: %s is not logged in on %s.\n"),
132 argv[1], argv[2]);
133 exit(1);
134 }
135 if (term_chk(argv[2], &msgsok, &atime, 1))
136 exit(1);
137 if (myuid && !msgsok) {
138 (void)fprintf(stderr,
139 _("write: %s has messages disabled on %s\n"),
140 argv[1], argv[2]);
141 exit(1);
142 }
143 do_write(argv[2], mytty, myuid);
144 break;
145 default:
146 (void)fprintf(stderr, _("usage: write user [tty]\n"));
147 exit(1);
148 }
149 done(0);
150 /* NOTREACHED */
151 return 0;
152 }
153
154
155 /*
156 * utmp_chk - checks that the given user is actually logged in on
157 * the given tty
158 */
159 int utmp_chk(char *user, char *tty)
160
161 {
162 struct utmp u;
163 struct utmp *uptr;
164 int res = 1;
165
166 utmpname(_PATH_UTMP);
167 setutent();
168
169 while ((uptr = getutent())) {
170 memcpy(&u, uptr, sizeof(u));
171 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
172 strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
173 res = 0;
174 break;
175 }
176 }
177
178 endutent();
179 return(res);
180 }
181
182 /*
183 * search_utmp - search utmp for the "best" terminal to write to
184 *
185 * Ignores terminals with messages disabled, and of the rest, returns
186 * the one with the most recent access time. Returns as value the number
187 * of the user's terminals with messages enabled, or -1 if the user is
188 * not logged in at all.
189 *
190 * Special case for writing to yourself - ignore the terminal you're
191 * writing from, unless that's the only terminal with messages enabled.
192 */
193 void search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
194
195 {
196 struct utmp u;
197 struct utmp *uptr;
198 time_t bestatime, atime;
199 int nloggedttys, nttys, msgsok, user_is_me;
200 #ifdef __linux__
201 char atty[sizeof(u.ut_line) + 1];
202 #else
203 char atty[UT_LINESIZE + 1];
204 #endif
205
206 utmpname(_PATH_UTMP);
207 setutent();
208
209 nloggedttys = nttys = 0;
210 bestatime = 0;
211 user_is_me = 0;
212 while ((uptr = getutent())) {
213 memcpy(&u, uptr, sizeof(u));
214 if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
215 ++nloggedttys;
216 #ifdef __linux__
217 (void)strncpy(atty, u.ut_line, sizeof(u.ut_line));
218 atty[sizeof(u.ut_line)] = '\0';
219 #else
220 (void)strncpy(atty, u.ut_line, UT_LINESIZE);
221 atty[UT_LINESIZE] = '\0';
222 #endif
223 if (term_chk(atty, &msgsok, &atime, 0))
224 continue; /* bad term? skip */
225 if (myuid && !msgsok)
226 continue; /* skip ttys with msgs off */
227 if (strcmp(atty, mytty) == 0) {
228 user_is_me = 1;
229 continue; /* don't write to yourself */
230 }
231 #ifdef __linux__
232 if (u.ut_type != USER_PROCESS)
233 continue; /* it's not a valid entry */
234 #endif
235 ++nttys;
236 if (atime > bestatime) {
237 bestatime = atime;
238 (void)strcpy(tty, atty);
239 }
240 }
241 }
242
243 endutent();
244 if (nloggedttys == 0) {
245 (void)fprintf(stderr, _("write: %s is not logged in\n"), user);
246 exit(1);
247 }
248 if (nttys == 0) {
249 if (user_is_me) { /* ok, so write to yourself! */
250 (void)strcpy(tty, mytty);
251 return;
252 }
253 (void)fprintf(stderr,
254 _("write: %s has messages disabled\n"), user);
255 exit(1);
256 } else if (nttys > 1) {
257 (void)fprintf(stderr,
258 _("write: %s is logged in more than once; writing to %s\n"),
259 user, tty);
260 }
261 }
262
263 /*
264 * term_chk - check that a terminal exists, and get the message bit
265 * and the access time
266 */
267 int term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
268
269 {
270 struct stat s;
271 char path[MAXPATHLEN];
272
273 if (strlen(tty) + 6 > sizeof(path))
274 return(1);
275 (void)sprintf(path, "/dev/%s", tty);
276 if (stat(path, &s) < 0) {
277 if (showerror)
278 (void)fprintf(stderr,
279 "write: %s: %s\n", path, strerror(errno));
280 return(1);
281 }
282 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */
283 *atimeP = s.st_atime;
284 return(0);
285 }
286
287 /*
288 * do_write - actually make the connection
289 */
290 void do_write(char *tty, char *mytty, uid_t myuid) {
291 char *login, *pwuid, *nows;
292 struct passwd *pwd;
293 time_t now;
294 char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
295
296 /* Determine our login name(s) before the we reopen() stdout */
297 if ((pwd = getpwuid(myuid)) != NULL)
298 pwuid = pwd->pw_name;
299 else
300 pwuid = "???";
301 if ((login = getlogin()) == NULL)
302 login = pwuid;
303
304 if (strlen(tty) + 6 > sizeof(path))
305 exit(1);
306 (void)sprintf(path, "/dev/%s", tty);
307 if ((freopen(path, "w", stdout)) == NULL) {
308 (void)fprintf(stderr, "write: %s: %s\n",
309 path, strerror(errno));
310 exit(1);
311 }
312
313 (void)signal(SIGINT, done);
314 (void)signal(SIGHUP, done);
315
316 /* print greeting */
317 if (gethostname(host, sizeof(host)) < 0)
318 (void)strcpy(host, "???");
319 now = time((time_t *)NULL);
320 nows = ctime(&now);
321 nows[16] = '\0';
322 printf("\r\n\007\007\007");
323 if (strcmp(login, pwuid))
324 (void)printf(_("Message from %s@%s (as %s) on %s at %s ..."),
325 login, host, pwuid, mytty, nows + 11);
326 else
327 (void)printf(_("Message from %s@%s on %s at %s ..."),
328 login, host, mytty, nows + 11);
329 printf("\r\n");
330
331 while (fgets(line, sizeof(line), stdin) != NULL)
332 wr_fputs(line);
333 }
334
335 /*
336 * done - cleanup and exit
337 */
338 static void
339 done(int dummy) {
340 (void)printf("EOF\r\n");
341 exit(0);
342 }
343
344 /*
345 * wr_fputs - like fputs(), but makes control characters visible and
346 * turns \n into \r\n.
347 */
348 void
349 wr_fputs(char *s) {
350 char c;
351
352 #define PUTC(c) if (carefulputc(c,stdout) == EOF) goto err;
353
354 while(*s) {
355 c = *s++;
356 if (c == '\n')
357 PUTC('\r');
358 PUTC(c);
359 }
360 return;
361
362 err:
363 fprintf(stderr, "write: %s\n", strerror(errno));
364 exit(1);
365 #undef PUTC
366 }