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