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