Home | History | Annotate | Line # | Download | only in write
      1  1.27     joerg /*	$NetBSD: write.c,v 1.27 2011/09/06 18:46:35 joerg 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.23       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35   1.9       mrg #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.25     lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
     38  1.25     lukem  The Regents of the University of California.  All rights reserved.");
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #ifndef lint
     42   1.5       jtc #if 0
     43   1.5       jtc static char sccsid[] = "@(#)write.c	8.2 (Berkeley) 4/27/95";
     44   1.9       mrg #else
     45  1.27     joerg __RCSID("$NetBSD: write.c,v 1.27 2011/09/06 18:46:35 joerg Exp $");
     46   1.5       jtc #endif
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49   1.4       jtc #include <sys/types.h>
     50   1.1       cgd #include <sys/param.h>
     51   1.1       cgd #include <sys/stat.h>
     52   1.1       cgd #include <ctype.h>
     53   1.1       cgd #include <stdio.h>
     54  1.17      matt #include <stdlib.h>
     55   1.1       cgd #include <string.h>
     56   1.4       jtc #include <signal.h>
     57   1.4       jtc #include <time.h>
     58   1.4       jtc #include <fcntl.h>
     59  1.18       mjl #include <paths.h>
     60   1.4       jtc #include <pwd.h>
     61   1.4       jtc #include <unistd.h>
     62   1.4       jtc #include <err.h>
     63  1.21    itojun #include <errno.h>
     64   1.1       cgd 
     65  1.20  christos #include "utmpentry.h"
     66  1.22  christos #include "term_chk.h"
     67  1.20  christos 
     68  1.27     joerg __dead static void done(int);
     69  1.27     joerg static void do_write(int, const char *, const uid_t);
     70  1.27     joerg static void wr_fputs(char *);
     71  1.27     joerg static int search_utmp(char *, char *, uid_t, gid_t);
     72  1.27     joerg static int utmp_chk(const char *, const char *);
     73  1.21    itojun 
     74   1.4       jtc int
     75  1.19       mjl main(int argc, char **argv)
     76   1.1       cgd {
     77   1.1       cgd 	time_t atime;
     78  1.21    itojun 	uid_t myuid, uid;
     79  1.22  christos 	int msgsok, ttyfd;
     80  1.21    itojun 	char *mytty;
     81  1.22  christos 	gid_t saved_egid = getegid();
     82  1.21    itojun 
     83  1.21    itojun 	if (setegid(getgid()) == -1)
     84  1.21    itojun 		err(1, "setegid");
     85  1.21    itojun 	myuid = getuid();
     86  1.21    itojun 	ttyfd = -1;
     87   1.1       cgd 
     88  1.22  christos 	mytty = check_sender(&atime, myuid, saved_egid);
     89   1.1       cgd 
     90   1.1       cgd 	/* check args */
     91   1.1       cgd 	switch (argc) {
     92   1.1       cgd 	case 2:
     93  1.22  christos 		ttyfd = search_utmp(argv[1], mytty, myuid, saved_egid);
     94   1.1       cgd 		break;
     95   1.1       cgd 	case 3:
     96  1.18       mjl 		if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV)))
     97  1.18       mjl 			argv[2] += strlen(_PATH_DEV);
     98  1.21    itojun 		if (uid_from_user(argv[1], &uid) == -1)
     99  1.21    itojun 			errx(1, "%s: unknown user", argv[1]);
    100   1.3   mycroft 		if (utmp_chk(argv[1], argv[2]))
    101   1.3   mycroft 			errx(1, "%s is not logged in on %s",
    102   1.1       cgd 			    argv[1], argv[2]);
    103  1.22  christos 		ttyfd = term_chk(uid, argv[2], &msgsok, &atime, 0, saved_egid);
    104  1.21    itojun 		if (ttyfd == -1)
    105  1.21    itojun 			err(1, "%s%s", _PATH_DEV, argv[2]);
    106   1.3   mycroft 		if (myuid && !msgsok)
    107   1.3   mycroft 			errx(1, "%s has messages disabled on %s",
    108   1.1       cgd 			    argv[1], argv[2]);
    109   1.1       cgd 		break;
    110   1.1       cgd 	default:
    111   1.1       cgd 		(void)fprintf(stderr, "usage: write user [tty]\n");
    112   1.1       cgd 		exit(1);
    113   1.1       cgd 	}
    114  1.21    itojun 	if (setgid(getgid()) == -1)
    115  1.21    itojun 		err(1, "setgid");
    116  1.21    itojun 	do_write(ttyfd, mytty, myuid);
    117   1.9       mrg 	done(0);
    118   1.1       cgd 	/* NOTREACHED */
    119   1.9       mrg #ifdef __GNUC__
    120   1.9       mrg 	return (0);
    121   1.9       mrg #endif
    122   1.1       cgd }
    123   1.1       cgd 
    124   1.1       cgd /*
    125   1.1       cgd  * utmp_chk - checks that the given user is actually logged in on
    126   1.1       cgd  *     the given tty
    127   1.1       cgd  */
    128  1.27     joerg static int
    129  1.19       mjl utmp_chk(const char *user, const char *tty)
    130   1.1       cgd {
    131  1.20  christos 	struct utmpentry *ep;
    132   1.1       cgd 
    133  1.20  christos 	(void)getutentries(NULL, &ep);
    134   1.1       cgd 
    135  1.20  christos 	for (; ep; ep = ep->next)
    136  1.20  christos 		if (strcmp(user, ep->name) == 0 && strcmp(tty, ep->line) == 0)
    137   1.1       cgd 			return(0);
    138   1.1       cgd 	return(1);
    139   1.1       cgd }
    140   1.1       cgd 
    141   1.1       cgd /*
    142   1.1       cgd  * search_utmp - search utmp for the "best" terminal to write to
    143   1.1       cgd  *
    144   1.1       cgd  * Ignores terminals with messages disabled, and of the rest, returns
    145   1.1       cgd  * the one with the most recent access time.  Returns as value the number
    146   1.1       cgd  * of the user's terminals with messages enabled, or -1 if the user is
    147   1.1       cgd  * not logged in at all.
    148   1.1       cgd  *
    149   1.1       cgd  * Special case for writing to yourself - ignore the terminal you're
    150   1.1       cgd  * writing from, unless that's the only terminal with messages enabled.
    151   1.1       cgd  */
    152  1.27     joerg static int
    153  1.22  christos search_utmp(char *user, char *mytty, uid_t myuid, gid_t saved_egid)
    154   1.1       cgd {
    155  1.21    itojun 	char tty[MAXPATHLEN];
    156   1.1       cgd 	time_t bestatime, atime;
    157  1.20  christos 	int nloggedttys, nttys, msgsok, user_is_me;
    158  1.20  christos 	struct utmpentry *ep;
    159  1.21    itojun 	int fd, nfd;
    160  1.21    itojun 	uid_t uid;
    161  1.21    itojun 
    162  1.21    itojun 	if (uid_from_user(user, &uid) == -1)
    163  1.21    itojun 		errx(1, "%s: unknown user", user);
    164   1.1       cgd 
    165  1.20  christos 	(void)getutentries(NULL, &ep);
    166   1.1       cgd 
    167   1.1       cgd 	nloggedttys = nttys = 0;
    168   1.1       cgd 	bestatime = 0;
    169   1.1       cgd 	user_is_me = 0;
    170  1.21    itojun 	fd = -1;
    171  1.20  christos 	for (; ep; ep = ep->next)
    172  1.20  christos 		if (strcmp(user, ep->name) == 0) {
    173   1.1       cgd 			++nloggedttys;
    174  1.22  christos 			nfd = term_chk(uid, ep->line, &msgsok, &atime, 0,
    175  1.22  christos 			    saved_egid);
    176  1.21    itojun 			if (nfd == -1)
    177   1.1       cgd 				continue;	/* bad term? skip */
    178  1.21    itojun 			if (myuid && !msgsok) {
    179  1.21    itojun 				close(nfd);
    180   1.1       cgd 				continue;	/* skip ttys with msgs off */
    181  1.21    itojun 			}
    182  1.20  christos 			if (strcmp(ep->line, mytty) == 0) {
    183   1.1       cgd 				user_is_me = 1;
    184  1.21    itojun 				if (fd == -1)
    185  1.21    itojun 					fd = nfd;
    186  1.21    itojun 				else
    187  1.21    itojun 					close(nfd);
    188   1.1       cgd 				continue;	/* don't write to yourself */
    189   1.1       cgd 			}
    190   1.1       cgd 			++nttys;
    191   1.1       cgd 			if (atime > bestatime) {
    192   1.1       cgd 				bestatime = atime;
    193  1.21    itojun 				(void)strlcpy(tty, ep->line, sizeof(tty));
    194  1.21    itojun 				close(fd);
    195  1.21    itojun 				fd = nfd;
    196  1.21    itojun 			} else
    197  1.21    itojun 				close(nfd);
    198   1.1       cgd 		}
    199   1.1       cgd 
    200   1.3   mycroft 	if (nloggedttys == 0)
    201   1.3   mycroft 		errx(1, "%s is not logged in", user);
    202   1.1       cgd 	if (nttys == 0) {
    203  1.21    itojun 		if (user_is_me)			/* ok, so write to yourself! */
    204  1.21    itojun 			return fd;
    205   1.3   mycroft 		errx(1, "%s has messages disabled", user);
    206   1.3   mycroft 	} else if (nttys > 1)
    207   1.3   mycroft 		warnx("%s is logged in more than once; writing to %s",
    208   1.1       cgd 		    user, tty);
    209  1.21    itojun 	return fd;
    210   1.1       cgd }
    211   1.1       cgd 
    212   1.1       cgd /*
    213   1.1       cgd  * do_write - actually make the connection
    214   1.1       cgd  */
    215  1.27     joerg static void
    216  1.21    itojun do_write(int ttyfd, const char *mytty, const uid_t myuid)
    217   1.1       cgd {
    218  1.13   mycroft 	const char *login;
    219  1.13   mycroft 	char *nows;
    220  1.10     lukem 	struct passwd *pwd;
    221   1.4       jtc 	time_t now;
    222  1.21    itojun 	char host[MAXHOSTNAMELEN + 1], line[512];
    223   1.1       cgd 
    224  1.21    itojun 	/* Determine our login name before we re-open stdout */
    225  1.14      ross 	if ((login = getlogin()) == NULL) {
    226   1.9       mrg 		if ((pwd = getpwuid(myuid)) != NULL)
    227   1.1       cgd 			login = pwd->pw_name;
    228  1.14      ross 		else	login = "???";
    229  1.14      ross 	}
    230  1.21    itojun 
    231  1.21    itojun 	if (dup2(ttyfd, STDOUT_FILENO) == -1)
    232  1.21    itojun 		err(1, "dup2");
    233   1.1       cgd 
    234   1.1       cgd 	(void)signal(SIGINT, done);
    235   1.1       cgd 	(void)signal(SIGHUP, done);
    236  1.21    itojun 	(void)close(ttyfd);
    237   1.1       cgd 
    238   1.1       cgd 	/* print greeting */
    239   1.1       cgd 	if (gethostname(host, sizeof(host)) < 0)
    240  1.24      elad 		(void)strlcpy(host, "???", sizeof(host));
    241  1.12       mrg 	else
    242  1.12       mrg 		host[sizeof(host) - 1] = '\0';
    243  1.26    plunky 	now = time(NULL);
    244   1.1       cgd 	nows = ctime(&now);
    245   1.1       cgd 	nows[16] = '\0';
    246  1.21    itojun 	(void)printf("\r\n\a\a\aMessage from %s@%s on %s at %s ...\r\n",
    247   1.1       cgd 	    login, host, mytty, nows + 11);
    248   1.1       cgd 
    249   1.1       cgd 	while (fgets(line, sizeof(line), stdin) != NULL)
    250   1.1       cgd 		wr_fputs(line);
    251   1.1       cgd }
    252   1.1       cgd 
    253   1.1       cgd /*
    254   1.1       cgd  * done - cleanup and exit
    255   1.1       cgd  */
    256  1.27     joerg static void
    257  1.21    itojun done(int signo)
    258   1.1       cgd {
    259  1.12       mrg 
    260  1.21    itojun 	(void)write(STDOUT_FILENO, "EOF\r\n", sizeof("EOF\r\n") - 1);
    261  1.21    itojun 	if (signo == 0)
    262  1.21    itojun 		exit(0);
    263  1.21    itojun 	else
    264  1.21    itojun 		_exit(0);
    265   1.1       cgd }
    266   1.1       cgd 
    267   1.1       cgd /*
    268   1.1       cgd  * wr_fputs - like fputs(), but makes control characters visible and
    269   1.1       cgd  *     turns \n into \r\n
    270   1.1       cgd  */
    271  1.27     joerg static void
    272  1.19       mjl wr_fputs(char *s)
    273   1.1       cgd {
    274  1.21    itojun 	unsigned char c;
    275   1.1       cgd 
    276   1.1       cgd #define	PUTC(c)	if (putchar(c) == EOF) goto err;
    277   1.1       cgd 
    278   1.1       cgd 	for (; *s != '\0'; ++s) {
    279   1.1       cgd 		c = toascii(*s);
    280   1.1       cgd 		if (c == '\n') {
    281   1.1       cgd 			PUTC('\r');
    282  1.21    itojun 		} else if (!isprint(c) && !isspace(c) && c != '\a') {
    283   1.1       cgd 			PUTC('^');
    284  1.21    itojun 			c ^= 0x40;	/* DEL to ?, others to alpha */
    285  1.21    itojun 		}
    286  1.21    itojun 		PUTC(c);
    287   1.1       cgd 	}
    288   1.1       cgd 	return;
    289   1.1       cgd 
    290  1.16  drochner err:	err(1, NULL);
    291   1.1       cgd #undef PUTC
    292   1.1       cgd }
    293