write.c revision 173572
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1989, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * This code is derived from software contributed to Berkeley by 61590Srgrimes * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory. 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 1. Redistributions of source code must retain the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer. 131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer in the 151590Srgrimes * documentation and/or other materials provided with the distribution. 161590Srgrimes * 3. All advertising materials mentioning features or use of this software 171590Srgrimes * must display the following acknowledgement: 181590Srgrimes * This product includes software developed by the University of 191590Srgrimes * California, Berkeley and its contributors. 201590Srgrimes * 4. Neither the name of the University nor the names of its contributors 211590Srgrimes * may be used to endorse or promote products derived from this software 221590Srgrimes * without specific prior written permission. 231590Srgrimes * 241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341590Srgrimes * SUCH DAMAGE. 351590Srgrimes */ 361590Srgrimes 371590Srgrimes#ifndef lint 3828794Scharnierstatic const char copyright[] = 391590Srgrimes"@(#) Copyright (c) 1989, 1993\n\ 401590Srgrimes The Regents of the University of California. All rights reserved.\n"; 4187682Smarkm#endif 421590Srgrimes 4391378Sdwmalone#if 0 441590Srgrimes#ifndef lint 4591378Sdwmalonestatic char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93"; 4628794Scharnier#endif 4791378Sdwmalone#endif 481590Srgrimes 4991378Sdwmalone#include <sys/cdefs.h> 5091378Sdwmalone__FBSDID("$FreeBSD: head/usr.bin/write/write.c 173572 2007-11-12 20:02:21Z jhb $"); 5191378Sdwmalone 521590Srgrimes#include <sys/param.h> 531590Srgrimes#include <sys/signal.h> 541590Srgrimes#include <sys/stat.h> 551590Srgrimes#include <sys/file.h> 561590Srgrimes#include <sys/time.h> 571590Srgrimes#include <ctype.h> 5828794Scharnier#include <err.h> 5929430Sache#include <locale.h> 6019190Salex#include <paths.h> 611590Srgrimes#include <pwd.h> 621590Srgrimes#include <stdio.h> 6391378Sdwmalone#include <stdlib.h> 641590Srgrimes#include <string.h> 6528794Scharnier#include <unistd.h> 6628794Scharnier#include <utmp.h> 671590Srgrimes 6892922Simpvoid done(int); 6992922Simpvoid do_write(char *, char *, uid_t); 7092922Simpstatic void usage(void); 7192922Simpint term_chk(char *, int *, time_t *, int); 7292922Simpvoid wr_fputs(unsigned char *s); 7392922Simpvoid search_utmp(char *, char *, char *, uid_t); 7492922Simpint utmp_chk(char *, char *); 751590Srgrimes 7628794Scharnierint 77102944Sdwmalonemain(int argc, char **argv) 781590Srgrimes{ 791590Srgrimes time_t atime; 801590Srgrimes uid_t myuid; 811590Srgrimes int msgsok, myttyfd; 8228794Scharnier char tty[MAXPATHLEN], *mytty; 831590Srgrimes 8429430Sache (void)setlocale(LC_CTYPE, ""); 8529430Sache 8697454Stjr while (getopt(argc, argv, "") != -1) 8797454Stjr usage(); 8897454Stjr argc -= optind; 8997454Stjr argv += optind; 9097454Stjr 911590Srgrimes /* check that sender has write enabled */ 921590Srgrimes if (isatty(fileno(stdin))) 931590Srgrimes myttyfd = fileno(stdin); 941590Srgrimes else if (isatty(fileno(stdout))) 951590Srgrimes myttyfd = fileno(stdout); 961590Srgrimes else if (isatty(fileno(stderr))) 971590Srgrimes myttyfd = fileno(stderr); 9828794Scharnier else 9928794Scharnier errx(1, "can't find your tty"); 10028794Scharnier if (!(mytty = ttyname(myttyfd))) 10128794Scharnier errx(1, "can't find your tty's name"); 102173572Sjhb if (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV))) 103173572Sjhb mytty += strlen(_PATH_DEV); 1041590Srgrimes if (term_chk(mytty, &msgsok, &atime, 1)) 1051590Srgrimes exit(1); 10628794Scharnier if (!msgsok) 10728794Scharnier errx(1, "you have write permission turned off"); 1081590Srgrimes 1091590Srgrimes myuid = getuid(); 1101590Srgrimes 1111590Srgrimes /* check args */ 1121590Srgrimes switch (argc) { 11397454Stjr case 1: 11497454Stjr search_utmp(argv[0], tty, mytty, myuid); 1151590Srgrimes do_write(tty, mytty, myuid); 1161590Srgrimes break; 11797454Stjr case 2: 11897454Stjr if (!strncmp(argv[1], _PATH_DEV, strlen(_PATH_DEV))) 11997454Stjr argv[1] += strlen(_PATH_DEV); 12097454Stjr if (utmp_chk(argv[0], argv[1])) 12197454Stjr errx(1, "%s is not logged in on %s", argv[0], argv[1]); 12297454Stjr if (term_chk(argv[1], &msgsok, &atime, 1)) 1231590Srgrimes exit(1); 12428794Scharnier if (myuid && !msgsok) 12597454Stjr errx(1, "%s has messages disabled on %s", argv[0], argv[1]); 12697454Stjr do_write(argv[1], mytty, myuid); 1271590Srgrimes break; 1281590Srgrimes default: 12928794Scharnier usage(); 1301590Srgrimes } 13129430Sache done(0); 13228794Scharnier return (0); 1331590Srgrimes} 1341590Srgrimes 13528794Scharnierstatic void 136102944Sdwmaloneusage(void) 13728794Scharnier{ 13828794Scharnier (void)fprintf(stderr, "usage: write user [tty]\n"); 13928794Scharnier exit(1); 14028794Scharnier} 14128794Scharnier 1421590Srgrimes/* 1431590Srgrimes * utmp_chk - checks that the given user is actually logged in on 1441590Srgrimes * the given tty 1451590Srgrimes */ 14628794Scharnierint 147102944Sdwmaloneutmp_chk(char *user, char *tty) 1481590Srgrimes{ 1491590Srgrimes struct utmp u; 1501590Srgrimes int ufd; 1511590Srgrimes 1521590Srgrimes if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 1531590Srgrimes return(0); /* ignore error, shouldn't happen anyway */ 1541590Srgrimes 1551590Srgrimes while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 1561590Srgrimes if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 && 1571590Srgrimes strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) { 1581590Srgrimes (void)close(ufd); 1591590Srgrimes return(0); 1601590Srgrimes } 1611590Srgrimes 1621590Srgrimes (void)close(ufd); 1631590Srgrimes return(1); 1641590Srgrimes} 1651590Srgrimes 1661590Srgrimes/* 1671590Srgrimes * search_utmp - search utmp for the "best" terminal to write to 1681590Srgrimes * 1691590Srgrimes * Ignores terminals with messages disabled, and of the rest, returns 1701590Srgrimes * the one with the most recent access time. Returns as value the number 1711590Srgrimes * of the user's terminals with messages enabled, or -1 if the user is 1721590Srgrimes * not logged in at all. 1731590Srgrimes * 1741590Srgrimes * Special case for writing to yourself - ignore the terminal you're 1751590Srgrimes * writing from, unless that's the only terminal with messages enabled. 1761590Srgrimes */ 17728794Scharniervoid 178102944Sdwmalonesearch_utmp(char *user, char *tty, char *mytty, uid_t myuid) 1791590Srgrimes{ 1801590Srgrimes struct utmp u; 1811590Srgrimes time_t bestatime, atime; 1821590Srgrimes int ufd, nloggedttys, nttys, msgsok, user_is_me; 1831590Srgrimes char atty[UT_LINESIZE + 1]; 1841590Srgrimes 18528794Scharnier if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 18628794Scharnier err(1, "utmp"); 1871590Srgrimes 1881590Srgrimes nloggedttys = nttys = 0; 1891590Srgrimes bestatime = 0; 1901590Srgrimes user_is_me = 0; 1911590Srgrimes while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 1921590Srgrimes if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) { 1931590Srgrimes ++nloggedttys; 1941590Srgrimes (void)strncpy(atty, u.ut_line, UT_LINESIZE); 1951590Srgrimes atty[UT_LINESIZE] = '\0'; 1961590Srgrimes if (term_chk(atty, &msgsok, &atime, 0)) 1971590Srgrimes continue; /* bad term? skip */ 1981590Srgrimes if (myuid && !msgsok) 1991590Srgrimes continue; /* skip ttys with msgs off */ 2001590Srgrimes if (strcmp(atty, mytty) == 0) { 2011590Srgrimes user_is_me = 1; 2021590Srgrimes continue; /* don't write to yourself */ 2031590Srgrimes } 2041590Srgrimes ++nttys; 2051590Srgrimes if (atime > bestatime) { 2061590Srgrimes bestatime = atime; 2071590Srgrimes (void)strcpy(tty, atty); 2081590Srgrimes } 2091590Srgrimes } 2101590Srgrimes 2111590Srgrimes (void)close(ufd); 21228794Scharnier if (nloggedttys == 0) 21328794Scharnier errx(1, "%s is not logged in", user); 2141590Srgrimes if (nttys == 0) { 2151590Srgrimes if (user_is_me) { /* ok, so write to yourself! */ 2161590Srgrimes (void)strcpy(tty, mytty); 2171590Srgrimes return; 2181590Srgrimes } 21928794Scharnier errx(1, "%s has messages disabled", user); 2201590Srgrimes } else if (nttys > 1) { 22128794Scharnier warnx("%s is logged in more than once; writing to %s", user, tty); 2221590Srgrimes } 2231590Srgrimes} 2241590Srgrimes 2251590Srgrimes/* 2261590Srgrimes * term_chk - check that a terminal exists, and get the message bit 2271590Srgrimes * and the access time 2281590Srgrimes */ 22928794Scharnierint 230102944Sdwmaloneterm_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror) 2311590Srgrimes{ 2321590Srgrimes struct stat s; 2331590Srgrimes char path[MAXPATHLEN]; 2341590Srgrimes 23519190Salex (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 2361590Srgrimes if (stat(path, &s) < 0) { 2371590Srgrimes if (showerror) 23828794Scharnier warn("%s", path); 2391590Srgrimes return(1); 2401590Srgrimes } 2411590Srgrimes *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */ 2421590Srgrimes *atimeP = s.st_atime; 2431590Srgrimes return(0); 2441590Srgrimes} 2451590Srgrimes 2461590Srgrimes/* 2471590Srgrimes * do_write - actually make the connection 2481590Srgrimes */ 24928794Scharniervoid 250102944Sdwmalonedo_write(char *tty, char *mytty, uid_t myuid) 2511590Srgrimes{ 25287682Smarkm const char *login; 25387682Smarkm char *nows; 25487682Smarkm struct passwd *pwd; 25528794Scharnier time_t now; 25628794Scharnier char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512]; 2571590Srgrimes 258139718Scognet /* Determine our login name before we reopen() stdout */ 25948566Sbillf if ((login = getlogin()) == NULL) { 26028794Scharnier if ((pwd = getpwuid(myuid))) 2611590Srgrimes login = pwd->pw_name; 2621590Srgrimes else 2631590Srgrimes login = "???"; 26448566Sbillf } 2651590Srgrimes 26619190Salex (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 26728794Scharnier if ((freopen(path, "w", stdout)) == NULL) 26828794Scharnier err(1, "%s", path); 2691590Srgrimes 2701590Srgrimes (void)signal(SIGINT, done); 2711590Srgrimes (void)signal(SIGHUP, done); 2721590Srgrimes 2731590Srgrimes /* print greeting */ 2741590Srgrimes if (gethostname(host, sizeof(host)) < 0) 2751590Srgrimes (void)strcpy(host, "???"); 2761590Srgrimes now = time((time_t *)NULL); 2771590Srgrimes nows = ctime(&now); 2781590Srgrimes nows[16] = '\0'; 2791590Srgrimes (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n", 2801590Srgrimes login, host, mytty, nows + 11); 2811590Srgrimes 2821590Srgrimes while (fgets(line, sizeof(line), stdin) != NULL) 2831590Srgrimes wr_fputs(line); 2841590Srgrimes} 2851590Srgrimes 2861590Srgrimes/* 2871590Srgrimes * done - cleanup and exit 2881590Srgrimes */ 2891590Srgrimesvoid 290102944Sdwmalonedone(int n __unused) 2911590Srgrimes{ 2921590Srgrimes (void)printf("EOF\r\n"); 2931590Srgrimes exit(0); 2941590Srgrimes} 2951590Srgrimes 2961590Srgrimes/* 2971590Srgrimes * wr_fputs - like fputs(), but makes control characters visible and 2981590Srgrimes * turns \n into \r\n 2991590Srgrimes */ 30028794Scharniervoid 301102944Sdwmalonewr_fputs(unsigned char *s) 3021590Srgrimes{ 3031590Srgrimes 30428794Scharnier#define PUTC(c) if (putchar(c) == EOF) err(1, NULL); 3051590Srgrimes 3061590Srgrimes for (; *s != '\0'; ++s) { 30711916Sache if (*s == '\n') { 3081590Srgrimes PUTC('\r'); 30929431Sache } else if (((*s & 0x80) && *s < 0xA0) || 31029431Sache /* disable upper controls */ 31129433Sache (!isprint(*s) && !isspace(*s) && 31229433Sache *s != '\a' && *s != '\b') 31329430Sache ) { 31412097Sache if (*s & 0x80) { 31512097Sache *s &= ~0x80; 31612097Sache PUTC('M'); 31712097Sache PUTC('-'); 31812097Sache } 31912097Sache if (iscntrl(*s)) { 32012097Sache *s ^= 0x40; 32112097Sache PUTC('^'); 32212097Sache } 32312097Sache } 32412097Sache PUTC(*s); 3251590Srgrimes } 3261590Srgrimes return; 3271590Srgrimes#undef PUTC 3281590Srgrimes} 329