Remove advertising header from all userland binaries.
[dragonfly.git] / usr.bin / write / 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  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
33  * @(#)write.c  8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.bin/write/write.c,v 1.12 1999/08/28 01:07:48 peter Exp $
35  * $DragonFly: src/usr.bin/write/write.c,v 1.7 2005/08/30 22:02:36 liamfoy Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/file.h>
41
42 #include <ctype.h>
43 #include <err.h>
44 #include <locale.h>
45 #include <paths.h>
46 #include <pwd.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <signal.h>
51 #include <time.h>
52 #include <unistd.h>
53 #include "utmpentry.h"
54
55 static void     done(int);
56 static void     do_write(const char *, const char *, uid_t, int *);
57 static void     usage(void);
58 static int      term_chk(const char *, int *, time_t *, int);
59 static void     wr_fputs(unsigned char *s);
60 static void     search_utmp(const char *, char *, size_t, const char *, uid_t);
61 static int      utmp_chk(const char *, const char *);
62
63 int
64 main(int argc, char **argv)
65 {
66         time_t atime;
67         uid_t myuid;
68         int msgsok, mymsgok, myttyfd;
69         char tty[MAXPATHLEN], *mytty;
70
71         setlocale(LC_CTYPE, "");
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 (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV)))
85                 mytty += strlen(_PATH_DEV);
86         if (term_chk(mytty, &mymsgok, &atime, 1))
87                 exit(1);
88         if (!mymsgok)
89                 warnx("you have write permission turned off (man mesg)");
90
91         myuid = getuid();
92
93         /* check args */
94         switch (argc) {
95         case 2:
96                 search_utmp(argv[1], tty, sizeof tty, mytty, myuid);
97                 do_write(tty, mytty, myuid, &mymsgok);
98                 break;
99         case 3:
100                 if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV)))
101                         argv[2] += strlen(_PATH_DEV);
102                 if (utmp_chk(argv[1], argv[2]))
103                         errx(1, "%s is not logged in on %s", argv[1], argv[2]);
104                 if (term_chk(argv[2], &msgsok, &atime, 1))
105                         exit(1);
106                 if (myuid && !msgsok)
107                         errx(1, "%s has messages disabled on %s", argv[1], argv[2]);
108                 do_write(argv[2], mytty, myuid, &mymsgok);
109                 break;
110         default:
111                 usage();
112         }
113         done(0);
114         return (0);
115 }
116
117 static void
118 usage(void)
119 {
120         fprintf(stderr, "usage: write user [tty]\n");
121         exit(1);
122 }
123
124 /*
125  * utmp_chk - checks that the given user is actually logged in on
126  *     the given tty
127  */
128 static int
129 utmp_chk(const char *user, const char *tty)
130 {
131         struct utmpentry *ep;
132
133         getutentries(NULL, &ep);
134
135         for (; ep; ep = ep->next) {
136                 if (strcmp(user, ep->name) == 0 &&
137                     strcmp(tty, ep->line) == 0) {
138                         return(0);
139                 }
140         }
141
142         return(1);
143 }
144
145 /*
146  * search_utmp - search utmp for the "best" terminal to write to
147  *
148  * Ignores terminals with messages disabled, and of the rest, returns
149  * the one with the most recent access time.  Returns as value the number
150  * of the user's terminals with messages enabled, or -1 if the user is
151  * not logged in at all.
152  *
153  * Special case for writing to yourself - ignore the terminal you're
154  * writing from, unless that's the only terminal with messages enabled.
155  */
156 static void
157 search_utmp(const char *user, char *tty, size_t ttyl, const char *mytty, uid_t myuid)
158 {
159         struct utmpentry *ep;
160         time_t bestatime, atime;
161         int nloggedttys, nttys, msgsok, user_is_me;
162
163         getutentries(NULL, &ep);
164
165         nloggedttys = nttys = 0;
166         bestatime = 0;
167         user_is_me = 0;
168         for (; ep; ep = ep->next)
169                 if (strcmp(user, ep->name) == 0) {
170                         ++nloggedttys;
171                         if (term_chk(ep->line, &msgsok, &atime, 0))
172                                 continue;       /* bad term? skip */
173                         if (myuid && !msgsok)
174                                 continue;       /* skip ttys with msgs off */
175                         if (strcmp(ep->line, mytty) == 0) {
176                                 user_is_me = 1;
177                                 continue;       /* don't write to yourself */
178                         }
179                         ++nttys;
180                         if (atime > bestatime) {
181                                 bestatime = atime;
182                                 strlcpy(tty, ep->line, ttyl);
183                         }
184                 }
185
186         if (nloggedttys == 0)
187                 errx(1, "%s is not logged in", user);
188         if (nttys == 0) {
189                 if (user_is_me) {               /* ok, so write to yourself! */
190                         strlcpy(tty, mytty, ttyl);
191                         return;
192                 }
193                 errx(1, "%s has messages disabled", user);
194         } else if (nttys > 1) {
195                 warnx("%s is logged in more than once; writing to %s", user, tty);
196         }
197 }
198
199 /*
200  * term_chk - check that a terminal exists, and get the message bit
201  *     and the access time
202  */
203 static int
204 term_chk(const char *tty, int *msgsokP, time_t *atimeP, int showerror)
205 {
206         struct stat s;
207         char path[MAXPATHLEN];
208
209         snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
210         if (stat(path, &s) < 0) {
211                 if (showerror)
212                         warn("%s", path);
213                 return(1);
214         }
215         *msgsokP = (s.st_mode & S_IWGRP) != 0;  /* group write bit */
216         *atimeP = s.st_atime;
217         return(0);
218 }
219
220 /*
221  * do_write - actually make the connection
222  */
223 static void
224 do_write(const char *tty, const char *mytty, uid_t myuid, int *mymsgok)
225 {
226         const char *login;
227         char *nows;
228         struct passwd *pwd;
229         time_t now;
230         char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
231
232         /* Determine our login name before we reopen() stdout */
233         if ((login = getlogin()) == NULL) {
234                 if ((pwd = getpwuid(myuid)))
235                         login = pwd->pw_name;
236                 else
237                         login = "???";
238         }
239
240         snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
241         if ((freopen(path, "w", stdout)) == NULL)
242                 err(1, "%s", path);
243
244         signal(SIGINT, done);
245         signal(SIGHUP, done);
246
247         /* print greeting */
248         if (gethostname(host, sizeof(host)) < 0)
249                 strlcpy(host, "???", sizeof host);
250         now = time(NULL);
251         nows = ctime(&now);
252         nows[16] = '\0';
253         printf("\r\n\007\007\007Message from %s@%s on %s at %s (%s %s replies)...\r\n",
254             login, host, mytty, nows + 11, login, *mymsgok == 0 ? "does not accept" : "accepts");
255
256         while (fgets(line, sizeof(line), stdin) != NULL)
257                 wr_fputs(line);
258 }
259
260 /*
261  * done - cleanup and exit
262  */
263 static void
264 done(__unused int n)
265 {
266         printf("EOF\r\n");
267         exit(0);
268 }
269
270 /*
271  * wr_fputs - like fputs(), but makes control characters visible and
272  *     turns \n into \r\n
273  */
274 static void
275 wr_fputs(unsigned char *s)
276 {
277
278 #define PUTC(c) if (putchar(c) == EOF) err(1, NULL);
279
280         for (; *s != '\0'; ++s) {
281                 if (*s == '\n') {
282                         PUTC('\r');
283                 } else if (((*s & 0x80) && *s < 0xA0) ||
284                            /* disable upper controls */
285                            (!isprint(*s) && !isspace(*s) &&
286                             *s != '\a' && *s != '\b')
287                           ) {
288                         if (*s & 0x80) {
289                                 *s &= ~0x80;
290                                 PUTC('M');
291                                 PUTC('-');
292                         }
293                         if (iscntrl(*s)) {
294                                 *s ^= 0x40;
295                                 PUTC('^');
296                         }
297                 }
298                 PUTC(*s);
299         }
300         return;
301 #undef PUTC
302 }