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