2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/capsicum.h>
50 #include <sys/filio.h>
51 #include <sys/signal.h>
55 #include <capsicum_helpers.h>
71 void do_write(int, char *, char *, const char *);
72 static void usage(void);
73 int term_chk(int, char *, int *, time_t *, int);
74 void wr_fputs(wchar_t *s);
75 void search_utmp(int, char *, char *, char *, uid_t);
76 int utmp_chk(char *, char *);
79 main(int argc, char **argv)
81 unsigned long cmds[] = { TIOCGETA, TIOCGWINSZ, FIODGNAME };
87 char tty[MAXPATHLEN], *mytty;
91 (void)setlocale(LC_CTYPE, "");
93 devfd = open(_PATH_DEV, O_RDONLY);
96 cap_rights_init(&rights, CAP_FCNTL, CAP_FSTAT, CAP_IOCTL, CAP_LOOKUP,
98 if (cap_rights_limit(devfd, &rights) < 0 && errno != ENOSYS)
99 err(1, "can't limit devfd rights");
102 * Can't use capsicum helpers here because we need the additional
105 cap_rights_init(&rights, CAP_FCNTL, CAP_FSTAT, CAP_IOCTL, CAP_READ,
107 if ((cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS) ||
108 (cap_rights_limit(STDOUT_FILENO, &rights) < 0 && errno != ENOSYS) ||
109 (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS) ||
110 (cap_ioctls_limit(STDIN_FILENO, cmds, nitems(cmds)) < 0 && errno != ENOSYS) ||
111 (cap_ioctls_limit(STDOUT_FILENO, cmds, nitems(cmds)) < 0 && errno != ENOSYS) ||
112 (cap_ioctls_limit(STDERR_FILENO, cmds, nitems(cmds)) < 0 && errno != ENOSYS) ||
113 (cap_fcntls_limit(STDIN_FILENO, CAP_FCNTL_GETFL) < 0 && errno != ENOSYS) ||
114 (cap_fcntls_limit(STDOUT_FILENO, CAP_FCNTL_GETFL) < 0 && errno != ENOSYS) ||
115 (cap_fcntls_limit(STDERR_FILENO, CAP_FCNTL_GETFL) < 0 && errno != ENOSYS))
116 err(1, "can't limit stdio rights");
118 caph_cache_catpages();
122 * Cache UTX database fds.
127 * Determine our login name before we reopen() stdout
128 * and before entering capability sandbox.
131 if ((login = getlogin()) == NULL) {
132 if ((pwd = getpwuid(myuid)))
133 login = pwd->pw_name;
138 if (cap_enter() < 0 && errno != ENOSYS)
141 while (getopt(argc, argv, "") != -1)
146 /* check that sender has write enabled */
147 if (isatty(fileno(stdin)))
148 myttyfd = fileno(stdin);
149 else if (isatty(fileno(stdout)))
150 myttyfd = fileno(stdout);
151 else if (isatty(fileno(stderr)))
152 myttyfd = fileno(stderr);
154 errx(1, "can't find your tty");
155 if (!(mytty = ttyname(myttyfd)))
156 errx(1, "can't find your tty's name");
157 if (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV)))
158 mytty += strlen(_PATH_DEV);
159 if (term_chk(devfd, mytty, &msgsok, &atime, 1))
162 errx(1, "you have write permission turned off");
167 search_utmp(devfd, argv[0], tty, mytty, myuid);
168 do_write(devfd, tty, mytty, login);
171 if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV)))
172 argv[1] += strlen(_PATH_DEV);
173 if (utmp_chk(argv[0], argv[1]))
174 errx(1, "%s is not logged in on %s", argv[0], argv[1]);
175 if (term_chk(devfd, argv[1], &msgsok, &atime, 1))
177 if (myuid && !msgsok)
178 errx(1, "%s has messages disabled on %s", argv[0], argv[1]);
179 do_write(devfd, argv[1], mytty, login);
191 (void)fprintf(stderr, "usage: write user [tty]\n");
196 * utmp_chk - checks that the given user is actually logged in on
200 utmp_chk(char *user, char *tty)
204 strncpy(lu.ut_line, tty, sizeof lu.ut_line);
206 while ((u = getutxline(&lu)) != NULL)
207 if (u->ut_type == USER_PROCESS &&
208 strcmp(user, u->ut_user) == 0) {
217 * search_utmp - search utmp for the "best" terminal to write to
219 * Ignores terminals with messages disabled, and of the rest, returns
220 * the one with the most recent access time. Returns as value the number
221 * of the user's terminals with messages enabled, or -1 if the user is
222 * not logged in at all.
224 * Special case for writing to yourself - ignore the terminal you're
225 * writing from, unless that's the only terminal with messages enabled.
228 search_utmp(int devfd, char *user, char *tty, char *mytty, uid_t myuid)
231 time_t bestatime, atime;
232 int nloggedttys, nttys, msgsok, user_is_me;
234 nloggedttys = nttys = 0;
239 while ((u = getutxent()) != NULL)
240 if (u->ut_type == USER_PROCESS &&
241 strcmp(user, u->ut_user) == 0) {
243 if (term_chk(devfd, u->ut_line, &msgsok, &atime, 0))
244 continue; /* bad term? skip */
245 if (myuid && !msgsok)
246 continue; /* skip ttys with msgs off */
247 if (strcmp(u->ut_line, mytty) == 0) {
249 continue; /* don't write to yourself */
252 if (atime > bestatime) {
254 (void)strlcpy(tty, u->ut_line, MAXPATHLEN);
259 if (nloggedttys == 0)
260 errx(1, "%s is not logged in", user);
262 if (user_is_me) { /* ok, so write to yourself! */
263 (void)strlcpy(tty, mytty, MAXPATHLEN);
266 errx(1, "%s has messages disabled", user);
267 } else if (nttys > 1) {
268 warnx("%s is logged in more than once; writing to %s", user, tty);
273 * term_chk - check that a terminal exists, and get the message bit
274 * and the access time
277 term_chk(int devfd, char *tty, int *msgsokP, time_t *atimeP, int showerror)
281 if (fstatat(devfd, tty, &s, 0) < 0) {
283 warn("%s%s", _PATH_DEV, tty);
286 *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */
287 *atimeP = s.st_atime;
292 * do_write - actually make the connection
295 do_write(int devfd, char *tty, char *mytty, const char *login)
299 char host[MAXHOSTNAMELEN];
303 fd = openat(devfd, tty, O_WRONLY);
305 err(1, "openat(%s%s)", _PATH_DEV, tty);
307 stdout = fdopen(fd, "w");
309 err(1, "%s%s", _PATH_DEV, tty);
311 (void)signal(SIGINT, done);
312 (void)signal(SIGHUP, done);
315 if (gethostname(host, sizeof(host)) < 0)
316 (void)strcpy(host, "???");
317 now = time((time_t *)NULL);
320 (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
321 login, host, mytty, nows + 11);
323 while (fgetws(line, sizeof(line)/sizeof(wchar_t), stdin) != NULL)
328 * done - cleanup and exit
333 (void)printf("EOF\r\n");
338 * wr_fputs - like fputs(), but makes control characters visible and
345 #define PUTC(c) if (putwchar(c) == WEOF) err(1, NULL);
347 for (; *s != L'\0'; ++s) {
351 } else if (iswprint(*s) || iswspace(*s)) {
354 wprintf(L"<0x%X>", *s);