Home | History | Annotate | Line # | Download | only in write
write.c revision 1.14
      1  1.14     ross /*	$NetBSD: write.c,v 1.14 1998/08/25 20:59:43 ross 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.14     ross __RCSID("$NetBSD: write.c,v 1.14 1998/08/25 20:59:43 ross 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.1      cgd #include <string.h>
     59   1.4      jtc #include <signal.h>
     60   1.4      jtc #include <time.h>
     61   1.4      jtc #include <fcntl.h>
     62   1.4      jtc #include <pwd.h>
     63   1.4      jtc #include <unistd.h>
     64   1.4      jtc #include <utmp.h>
     65   1.4      jtc #include <err.h>
     66   1.1      cgd 
     67   1.9      mrg void done __P((int));
     68   1.4      jtc void do_write __P((char *, char *, uid_t));
     69   1.4      jtc void wr_fputs __P((char *));
     70   1.8      mrg void search_utmp __P((char *, char *, char *, uid_t, int));
     71   1.4      jtc int term_chk __P((char *, int *, time_t *, int));
     72   1.4      jtc int utmp_chk __P((char *, char *));
     73   1.9      mrg int main __P((int, char **));
     74   1.1      cgd 
     75   1.4      jtc int
     76   1.1      cgd main(argc, argv)
     77   1.1      cgd 	int argc;
     78   1.1      cgd 	char **argv;
     79   1.1      cgd {
     80  1.10    lukem 	char *cp;
     81   1.1      cgd 	time_t atime;
     82   1.1      cgd 	uid_t myuid;
     83   1.1      cgd 	int msgsok, myttyfd;
     84   1.4      jtc 	char tty[MAXPATHLEN], *mytty;
     85   1.1      cgd 
     86   1.1      cgd 	/* check that sender has write enabled */
     87   1.1      cgd 	if (isatty(fileno(stdin)))
     88   1.1      cgd 		myttyfd = fileno(stdin);
     89   1.1      cgd 	else if (isatty(fileno(stdout)))
     90   1.1      cgd 		myttyfd = fileno(stdout);
     91   1.1      cgd 	else if (isatty(fileno(stderr)))
     92   1.1      cgd 		myttyfd = fileno(stderr);
     93   1.3  mycroft 	else
     94   1.3  mycroft 		errx(1, "can't find your tty");
     95   1.3  mycroft 	if (!(mytty = ttyname(myttyfd)))
     96   1.3  mycroft 		errx(1, "can't find your tty's name");
     97   1.9      mrg 	if ((cp = strrchr(mytty, '/')) != NULL)
     98   1.1      cgd 		mytty = cp + 1;
     99   1.1      cgd 	if (term_chk(mytty, &msgsok, &atime, 1))
    100   1.1      cgd 		exit(1);
    101   1.6    perry 	if (!msgsok) {
    102   1.6    perry 		(void)fprintf(stderr,
    103   1.6    perry 		    "warning: you have write permission turned off; "
    104   1.6    perry 		    "no reply possible\n");
    105   1.6    perry 	}
    106   1.1      cgd 
    107   1.1      cgd 	myuid = getuid();
    108   1.1      cgd 
    109   1.1      cgd 	/* check args */
    110   1.1      cgd 	switch (argc) {
    111   1.1      cgd 	case 2:
    112   1.8      mrg 		search_utmp(argv[1], tty, mytty, myuid, sizeof tty);
    113   1.1      cgd 		do_write(tty, mytty, myuid);
    114   1.1      cgd 		break;
    115   1.1      cgd 	case 3:
    116   1.1      cgd 		if (!strncmp(argv[2], "/dev/", 5))
    117   1.1      cgd 			argv[2] += 5;
    118   1.3  mycroft 		if (utmp_chk(argv[1], argv[2]))
    119   1.3  mycroft 			errx(1, "%s is not logged in on %s",
    120   1.1      cgd 			    argv[1], argv[2]);
    121   1.1      cgd 		if (term_chk(argv[2], &msgsok, &atime, 1))
    122   1.1      cgd 			exit(1);
    123   1.3  mycroft 		if (myuid && !msgsok)
    124   1.3  mycroft 			errx(1, "%s has messages disabled on %s",
    125   1.1      cgd 			    argv[1], argv[2]);
    126   1.1      cgd 		do_write(argv[2], mytty, myuid);
    127   1.1      cgd 		break;
    128   1.1      cgd 	default:
    129   1.1      cgd 		(void)fprintf(stderr, "usage: write user [tty]\n");
    130   1.1      cgd 		exit(1);
    131   1.1      cgd 	}
    132   1.9      mrg 	done(0);
    133   1.1      cgd 	/* NOTREACHED */
    134   1.9      mrg #ifdef __GNUC__
    135   1.9      mrg 	return (0);
    136   1.9      mrg #endif
    137   1.1      cgd }
    138   1.1      cgd 
    139   1.1      cgd /*
    140   1.1      cgd  * utmp_chk - checks that the given user is actually logged in on
    141   1.1      cgd  *     the given tty
    142   1.1      cgd  */
    143   1.4      jtc int
    144   1.1      cgd utmp_chk(user, tty)
    145   1.1      cgd 	char *user, *tty;
    146   1.1      cgd {
    147   1.1      cgd 	struct utmp u;
    148   1.1      cgd 	int ufd;
    149   1.1      cgd 
    150   1.1      cgd 	if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
    151   1.1      cgd 		return(0);	/* ignore error, shouldn't happen anyway */
    152   1.1      cgd 
    153   1.1      cgd 	while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
    154   1.1      cgd 		if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
    155   1.1      cgd 		    strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
    156   1.1      cgd 			(void)close(ufd);
    157   1.1      cgd 			return(0);
    158   1.1      cgd 		}
    159   1.1      cgd 
    160   1.1      cgd 	(void)close(ufd);
    161   1.1      cgd 	return(1);
    162   1.1      cgd }
    163   1.1      cgd 
    164   1.1      cgd /*
    165   1.1      cgd  * search_utmp - search utmp for the "best" terminal to write to
    166   1.1      cgd  *
    167   1.1      cgd  * Ignores terminals with messages disabled, and of the rest, returns
    168   1.1      cgd  * the one with the most recent access time.  Returns as value the number
    169   1.1      cgd  * of the user's terminals with messages enabled, or -1 if the user is
    170   1.1      cgd  * not logged in at all.
    171   1.1      cgd  *
    172   1.1      cgd  * Special case for writing to yourself - ignore the terminal you're
    173   1.1      cgd  * writing from, unless that's the only terminal with messages enabled.
    174   1.1      cgd  */
    175   1.4      jtc void
    176   1.8      mrg search_utmp(user, tty, mytty, myuid, ttylen)
    177   1.1      cgd 	char *user, *tty, *mytty;
    178   1.1      cgd 	uid_t myuid;
    179   1.8      mrg 	int ttylen;
    180   1.1      cgd {
    181   1.1      cgd 	struct utmp u;
    182   1.1      cgd 	time_t bestatime, atime;
    183   1.1      cgd 	int ufd, nloggedttys, nttys, msgsok, user_is_me;
    184   1.1      cgd 	char atty[UT_LINESIZE + 1];
    185   1.1      cgd 
    186   1.3  mycroft 	if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
    187   1.3  mycroft 		err(1, "%s", _PATH_UTMP);
    188   1.1      cgd 
    189   1.1      cgd 	nloggedttys = nttys = 0;
    190   1.1      cgd 	bestatime = 0;
    191   1.1      cgd 	user_is_me = 0;
    192   1.1      cgd 	while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
    193   1.1      cgd 		if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
    194   1.1      cgd 			++nloggedttys;
    195   1.1      cgd 			(void)strncpy(atty, u.ut_line, UT_LINESIZE);
    196   1.1      cgd 			atty[UT_LINESIZE] = '\0';
    197   1.1      cgd 			if (term_chk(atty, &msgsok, &atime, 0))
    198   1.1      cgd 				continue;	/* bad term? skip */
    199   1.1      cgd 			if (myuid && !msgsok)
    200   1.1      cgd 				continue;	/* skip ttys with msgs off */
    201   1.1      cgd 			if (strcmp(atty, mytty) == 0) {
    202   1.1      cgd 				user_is_me = 1;
    203   1.1      cgd 				continue;	/* don't write to yourself */
    204   1.1      cgd 			}
    205   1.1      cgd 			++nttys;
    206   1.1      cgd 			if (atime > bestatime) {
    207   1.1      cgd 				bestatime = atime;
    208   1.8      mrg 				(void)strncpy(tty, atty, ttylen - 1);
    209  1.12      mrg 				tty[ttylen - 1] = '\0';
    210   1.1      cgd 			}
    211   1.1      cgd 		}
    212   1.1      cgd 
    213   1.1      cgd 	(void)close(ufd);
    214   1.3  mycroft 	if (nloggedttys == 0)
    215   1.3  mycroft 		errx(1, "%s is not logged in", user);
    216   1.1      cgd 	if (nttys == 0) {
    217   1.1      cgd 		if (user_is_me) {		/* ok, so write to yourself! */
    218   1.8      mrg 			(void)strncpy(tty, mytty, ttylen - 1);
    219  1.12      mrg 			tty[ttylen - 1] = '\0';
    220   1.1      cgd 			return;
    221   1.1      cgd 		}
    222   1.3  mycroft 		errx(1, "%s has messages disabled", user);
    223   1.3  mycroft 	} else if (nttys > 1)
    224   1.3  mycroft 		warnx("%s is logged in more than once; writing to %s",
    225   1.1      cgd 		    user, tty);
    226   1.1      cgd }
    227   1.1      cgd 
    228   1.1      cgd /*
    229   1.1      cgd  * term_chk - check that a terminal exists, and get the message bit
    230   1.1      cgd  *     and the access time
    231   1.1      cgd  */
    232   1.4      jtc int
    233   1.1      cgd term_chk(tty, msgsokP, atimeP, showerror)
    234   1.1      cgd 	char *tty;
    235   1.1      cgd 	int *msgsokP, showerror;
    236   1.1      cgd 	time_t *atimeP;
    237   1.1      cgd {
    238   1.1      cgd 	struct stat s;
    239   1.1      cgd 	char path[MAXPATHLEN];
    240   1.1      cgd 
    241   1.8      mrg 	(void)snprintf(path, sizeof path, "/dev/%s", tty);
    242   1.1      cgd 	if (stat(path, &s) < 0) {
    243   1.1      cgd 		if (showerror)
    244   1.3  mycroft 			warn("%s", path);
    245   1.1      cgd 		return(1);
    246   1.1      cgd 	}
    247  1.12      mrg 	*msgsokP = (s.st_mode & S_IWGRP) != 0;	/* group write bit */
    248   1.1      cgd 	*atimeP = s.st_atime;
    249   1.1      cgd 	return(0);
    250   1.1      cgd }
    251   1.1      cgd 
    252   1.1      cgd /*
    253   1.1      cgd  * do_write - actually make the connection
    254   1.1      cgd  */
    255   1.4      jtc void
    256   1.1      cgd do_write(tty, mytty, myuid)
    257   1.1      cgd 	char *tty, *mytty;
    258   1.1      cgd 	uid_t myuid;
    259   1.1      cgd {
    260  1.13  mycroft 	const char *login;
    261  1.13  mycroft 	char *nows;
    262  1.10    lukem 	struct passwd *pwd;
    263   1.4      jtc 	time_t now;
    264  1.11      mrg 	char path[MAXPATHLEN], host[MAXHOSTNAMELEN + 1], line[512];
    265   1.1      cgd 
    266   1.1      cgd 	/* Determine our login name before the we reopen() stdout */
    267  1.14     ross 	if ((login = getlogin()) == NULL) {
    268   1.9      mrg 		if ((pwd = getpwuid(myuid)) != NULL)
    269   1.1      cgd 			login = pwd->pw_name;
    270  1.14     ross 		else	login = "???";
    271  1.14     ross 	}
    272   1.8      mrg 	(void)snprintf(path, sizeof path, "/dev/%s", tty);
    273   1.3  mycroft 	if ((freopen(path, "w", stdout)) == NULL)
    274   1.3  mycroft 		err(1, "%s", path);
    275   1.1      cgd 
    276   1.1      cgd 	(void)signal(SIGINT, done);
    277   1.1      cgd 	(void)signal(SIGHUP, done);
    278   1.1      cgd 
    279   1.1      cgd 	/* print greeting */
    280   1.1      cgd 	if (gethostname(host, sizeof(host)) < 0)
    281   1.8      mrg 		(void)strncpy(host, "???", sizeof(host) - 1);
    282  1.12      mrg 	else
    283  1.12      mrg 		host[sizeof(host) - 1] = '\0';
    284   1.1      cgd 	now = time((time_t *)NULL);
    285   1.1      cgd 	nows = ctime(&now);
    286   1.1      cgd 	nows[16] = '\0';
    287   1.1      cgd 	(void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
    288   1.1      cgd 	    login, host, mytty, nows + 11);
    289   1.1      cgd 
    290   1.1      cgd 	while (fgets(line, sizeof(line), stdin) != NULL)
    291   1.1      cgd 		wr_fputs(line);
    292   1.1      cgd }
    293   1.1      cgd 
    294   1.1      cgd /*
    295   1.1      cgd  * done - cleanup and exit
    296   1.1      cgd  */
    297   1.1      cgd void
    298   1.9      mrg done(dummy)
    299   1.9      mrg 	int dummy;
    300   1.1      cgd {
    301  1.12      mrg 
    302   1.1      cgd 	(void)printf("EOF\r\n");
    303   1.1      cgd 	exit(0);
    304   1.1      cgd }
    305   1.1      cgd 
    306   1.1      cgd /*
    307   1.1      cgd  * wr_fputs - like fputs(), but makes control characters visible and
    308   1.1      cgd  *     turns \n into \r\n
    309   1.1      cgd  */
    310   1.4      jtc void
    311   1.1      cgd wr_fputs(s)
    312  1.10    lukem 	char *s;
    313   1.1      cgd {
    314  1.10    lukem 	char c;
    315   1.1      cgd 
    316   1.1      cgd #define	PUTC(c)	if (putchar(c) == EOF) goto err;
    317   1.1      cgd 
    318   1.1      cgd 	for (; *s != '\0'; ++s) {
    319   1.1      cgd 		c = toascii(*s);
    320   1.1      cgd 		if (c == '\n') {
    321   1.1      cgd 			PUTC('\r');
    322   1.1      cgd 			PUTC('\n');
    323   1.1      cgd 		} else if (!isprint(c) && !isspace(c) && c != '\007') {
    324   1.1      cgd 			PUTC('^');
    325   1.1      cgd 			PUTC(c^0x40);	/* DEL to ?, others to alpha */
    326   1.1      cgd 		} else
    327   1.1      cgd 			PUTC(c);
    328   1.1      cgd 	}
    329   1.1      cgd 	return;
    330   1.1      cgd 
    331   1.9      mrg err:	err(1, "%s", "");
    332   1.1      cgd #undef PUTC
    333   1.1      cgd }
    334