write.c revision 29433
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"; 411590Srgrimes#endif /* not lint */ 421590Srgrimes 431590Srgrimes#ifndef lint 4428794Scharnier#if 0 451590Srgrimesstatic char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93"; 4628794Scharnier#endif 4728794Scharnierstatic const char rcsid[] = 4829433Sache "$Id: write.c,v 1.9 1997/09/15 00:17:38 ache Exp $"; 491590Srgrimes#endif /* not lint */ 501590Srgrimes 511590Srgrimes#include <sys/param.h> 521590Srgrimes#include <sys/signal.h> 531590Srgrimes#include <sys/stat.h> 541590Srgrimes#include <sys/file.h> 551590Srgrimes#include <sys/time.h> 561590Srgrimes#include <ctype.h> 5728794Scharnier#include <err.h> 5829430Sache#include <locale.h> 5919190Salex#include <paths.h> 601590Srgrimes#include <pwd.h> 611590Srgrimes#include <stdio.h> 621590Srgrimes#include <string.h> 6328794Scharnier#include <unistd.h> 6428794Scharnier#include <utmp.h> 651590Srgrimes 6629430Sachevoid done __P((int)); 6728794Scharniervoid do_write __P((char *, char *, uid_t)); 6828794Scharnierstatic void usage __P((void)); 6928794Scharnierint term_chk __P((char *, int *, time_t *, int)); 7028794Scharniervoid wr_fputs __P((unsigned char *s)); 7128794Scharniervoid search_utmp __P((char *, char *, char *, uid_t)); 7228794Scharnierint utmp_chk __P((char *, char *)); 731590Srgrimes 7428794Scharnierint 751590Srgrimesmain(argc, argv) 761590Srgrimes int argc; 771590Srgrimes char **argv; 781590Srgrimes{ 791590Srgrimes register char *cp; 801590Srgrimes time_t atime; 811590Srgrimes uid_t myuid; 821590Srgrimes int msgsok, myttyfd; 8328794Scharnier char tty[MAXPATHLEN], *mytty; 841590Srgrimes 8529430Sache (void)setlocale(LC_CTYPE, ""); 8629430Sache 871590Srgrimes /* check that sender has write enabled */ 881590Srgrimes if (isatty(fileno(stdin))) 891590Srgrimes myttyfd = fileno(stdin); 901590Srgrimes else if (isatty(fileno(stdout))) 911590Srgrimes myttyfd = fileno(stdout); 921590Srgrimes else if (isatty(fileno(stderr))) 931590Srgrimes myttyfd = fileno(stderr); 9428794Scharnier else 9528794Scharnier errx(1, "can't find your tty"); 9628794Scharnier if (!(mytty = ttyname(myttyfd))) 9728794Scharnier errx(1, "can't find your tty's name"); 9828794Scharnier if ((cp = rindex(mytty, '/'))) 991590Srgrimes mytty = cp + 1; 1001590Srgrimes if (term_chk(mytty, &msgsok, &atime, 1)) 1011590Srgrimes exit(1); 10228794Scharnier if (!msgsok) 10328794Scharnier errx(1, "you have write permission turned off"); 1041590Srgrimes 1051590Srgrimes myuid = getuid(); 1061590Srgrimes 1071590Srgrimes /* check args */ 1081590Srgrimes switch (argc) { 1091590Srgrimes case 2: 1101590Srgrimes search_utmp(argv[1], tty, mytty, myuid); 1111590Srgrimes do_write(tty, mytty, myuid); 1121590Srgrimes break; 1131590Srgrimes case 3: 11419190Salex if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV))) 11519193Salex argv[2] += strlen(_PATH_DEV); 11628794Scharnier if (utmp_chk(argv[1], argv[2])) 11728794Scharnier errx(1, "%s is not logged in on %s", argv[1], argv[2]); 1181590Srgrimes if (term_chk(argv[2], &msgsok, &atime, 1)) 1191590Srgrimes exit(1); 12028794Scharnier if (myuid && !msgsok) 12128794Scharnier errx(1, "%s has messages disabled on %s", argv[1], argv[2]); 1221590Srgrimes do_write(argv[2], mytty, myuid); 1231590Srgrimes break; 1241590Srgrimes default: 12528794Scharnier usage(); 1261590Srgrimes } 12729430Sache done(0); 12828794Scharnier return (0); 1291590Srgrimes} 1301590Srgrimes 13128794Scharnierstatic void 13228794Scharnierusage() 13328794Scharnier{ 13428794Scharnier (void)fprintf(stderr, "usage: write user [tty]\n"); 13528794Scharnier exit(1); 13628794Scharnier} 13728794Scharnier 1381590Srgrimes/* 1391590Srgrimes * utmp_chk - checks that the given user is actually logged in on 1401590Srgrimes * the given tty 1411590Srgrimes */ 14228794Scharnierint 1431590Srgrimesutmp_chk(user, tty) 1441590Srgrimes char *user, *tty; 1451590Srgrimes{ 1461590Srgrimes struct utmp u; 1471590Srgrimes int ufd; 1481590Srgrimes 1491590Srgrimes if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 1501590Srgrimes return(0); /* ignore error, shouldn't happen anyway */ 1511590Srgrimes 1521590Srgrimes while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 1531590Srgrimes if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 && 1541590Srgrimes strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) { 1551590Srgrimes (void)close(ufd); 1561590Srgrimes return(0); 1571590Srgrimes } 1581590Srgrimes 1591590Srgrimes (void)close(ufd); 1601590Srgrimes return(1); 1611590Srgrimes} 1621590Srgrimes 1631590Srgrimes/* 1641590Srgrimes * search_utmp - search utmp for the "best" terminal to write to 1651590Srgrimes * 1661590Srgrimes * Ignores terminals with messages disabled, and of the rest, returns 1671590Srgrimes * the one with the most recent access time. Returns as value the number 1681590Srgrimes * of the user's terminals with messages enabled, or -1 if the user is 1691590Srgrimes * not logged in at all. 1701590Srgrimes * 1711590Srgrimes * Special case for writing to yourself - ignore the terminal you're 1721590Srgrimes * writing from, unless that's the only terminal with messages enabled. 1731590Srgrimes */ 17428794Scharniervoid 1751590Srgrimessearch_utmp(user, tty, mytty, myuid) 1761590Srgrimes char *user, *tty, *mytty; 1771590Srgrimes uid_t myuid; 1781590Srgrimes{ 1791590Srgrimes struct utmp u; 1801590Srgrimes time_t bestatime, atime; 1811590Srgrimes int ufd, nloggedttys, nttys, msgsok, user_is_me; 1821590Srgrimes char atty[UT_LINESIZE + 1]; 1831590Srgrimes 18428794Scharnier if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 18528794Scharnier err(1, "utmp"); 1861590Srgrimes 1871590Srgrimes nloggedttys = nttys = 0; 1881590Srgrimes bestatime = 0; 1891590Srgrimes user_is_me = 0; 1901590Srgrimes while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 1911590Srgrimes if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) { 1921590Srgrimes ++nloggedttys; 1931590Srgrimes (void)strncpy(atty, u.ut_line, UT_LINESIZE); 1941590Srgrimes atty[UT_LINESIZE] = '\0'; 1951590Srgrimes if (term_chk(atty, &msgsok, &atime, 0)) 1961590Srgrimes continue; /* bad term? skip */ 1971590Srgrimes if (myuid && !msgsok) 1981590Srgrimes continue; /* skip ttys with msgs off */ 1991590Srgrimes if (strcmp(atty, mytty) == 0) { 2001590Srgrimes user_is_me = 1; 2011590Srgrimes continue; /* don't write to yourself */ 2021590Srgrimes } 2031590Srgrimes ++nttys; 2041590Srgrimes if (atime > bestatime) { 2051590Srgrimes bestatime = atime; 2061590Srgrimes (void)strcpy(tty, atty); 2071590Srgrimes } 2081590Srgrimes } 2091590Srgrimes 2101590Srgrimes (void)close(ufd); 21128794Scharnier if (nloggedttys == 0) 21228794Scharnier errx(1, "%s is not logged in", user); 2131590Srgrimes if (nttys == 0) { 2141590Srgrimes if (user_is_me) { /* ok, so write to yourself! */ 2151590Srgrimes (void)strcpy(tty, mytty); 2161590Srgrimes return; 2171590Srgrimes } 21828794Scharnier errx(1, "%s has messages disabled", user); 2191590Srgrimes } else if (nttys > 1) { 22028794Scharnier warnx("%s is logged in more than once; writing to %s", user, tty); 2211590Srgrimes } 2221590Srgrimes} 2231590Srgrimes 2241590Srgrimes/* 2251590Srgrimes * term_chk - check that a terminal exists, and get the message bit 2261590Srgrimes * and the access time 2271590Srgrimes */ 22828794Scharnierint 2291590Srgrimesterm_chk(tty, msgsokP, atimeP, showerror) 2301590Srgrimes char *tty; 2311590Srgrimes int *msgsokP, showerror; 2321590Srgrimes time_t *atimeP; 2331590Srgrimes{ 2341590Srgrimes struct stat s; 2351590Srgrimes char path[MAXPATHLEN]; 2361590Srgrimes 23719190Salex (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 2381590Srgrimes if (stat(path, &s) < 0) { 2391590Srgrimes if (showerror) 24028794Scharnier warn("%s", path); 2411590Srgrimes return(1); 2421590Srgrimes } 2431590Srgrimes *msgsokP = (s.st_mode & (S_IWRITE >> 3)) != 0; /* group write bit */ 2441590Srgrimes *atimeP = s.st_atime; 2451590Srgrimes return(0); 2461590Srgrimes} 2471590Srgrimes 2481590Srgrimes/* 2491590Srgrimes * do_write - actually make the connection 2501590Srgrimes */ 25128794Scharniervoid 2521590Srgrimesdo_write(tty, mytty, myuid) 2531590Srgrimes char *tty, *mytty; 2541590Srgrimes uid_t myuid; 2551590Srgrimes{ 2561590Srgrimes register char *login, *nows; 2571590Srgrimes register struct passwd *pwd; 25828794Scharnier time_t now; 25928794Scharnier char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512]; 2601590Srgrimes 2611590Srgrimes /* Determine our login name before the we reopen() stdout */ 2621590Srgrimes if ((login = getlogin()) == NULL) 26328794Scharnier if ((pwd = getpwuid(myuid))) 2641590Srgrimes login = pwd->pw_name; 2651590Srgrimes else 2661590Srgrimes login = "???"; 2671590Srgrimes 26819190Salex (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 26928794Scharnier if ((freopen(path, "w", stdout)) == NULL) 27028794Scharnier err(1, "%s", path); 2711590Srgrimes 2721590Srgrimes (void)signal(SIGINT, done); 2731590Srgrimes (void)signal(SIGHUP, done); 2741590Srgrimes 2751590Srgrimes /* print greeting */ 2761590Srgrimes if (gethostname(host, sizeof(host)) < 0) 2771590Srgrimes (void)strcpy(host, "???"); 2781590Srgrimes now = time((time_t *)NULL); 2791590Srgrimes nows = ctime(&now); 2801590Srgrimes nows[16] = '\0'; 2811590Srgrimes (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n", 2821590Srgrimes login, host, mytty, nows + 11); 2831590Srgrimes 2841590Srgrimes while (fgets(line, sizeof(line), stdin) != NULL) 2851590Srgrimes wr_fputs(line); 2861590Srgrimes} 2871590Srgrimes 2881590Srgrimes/* 2891590Srgrimes * done - cleanup and exit 2901590Srgrimes */ 2911590Srgrimesvoid 29229430Sachedone(n) 29329430Sacheint n; /* signal number */ 2941590Srgrimes{ 2951590Srgrimes (void)printf("EOF\r\n"); 2961590Srgrimes exit(0); 2971590Srgrimes} 2981590Srgrimes 2991590Srgrimes/* 3001590Srgrimes * wr_fputs - like fputs(), but makes control characters visible and 3011590Srgrimes * turns \n into \r\n 3021590Srgrimes */ 30328794Scharniervoid 3041590Srgrimeswr_fputs(s) 30512097Sache register unsigned char *s; 3061590Srgrimes{ 3071590Srgrimes 30828794Scharnier#define PUTC(c) if (putchar(c) == EOF) err(1, NULL); 3091590Srgrimes 3101590Srgrimes for (; *s != '\0'; ++s) { 31111916Sache if (*s == '\n') { 3121590Srgrimes PUTC('\r'); 31329431Sache } else if (((*s & 0x80) && *s < 0xA0) || 31429431Sache /* disable upper controls */ 31529433Sache (!isprint(*s) && !isspace(*s) && 31629433Sache *s != '\a' && *s != '\b') 31729430Sache ) { 31812097Sache if (*s & 0x80) { 31912097Sache *s &= ~0x80; 32012097Sache PUTC('M'); 32112097Sache PUTC('-'); 32212097Sache } 32312097Sache if (iscntrl(*s)) { 32412097Sache *s ^= 0x40; 32512097Sache PUTC('^'); 32612097Sache } 32712097Sache } 32812097Sache PUTC(*s); 3291590Srgrimes } 3301590Srgrimes return; 3311590Srgrimes#undef PUTC 3321590Srgrimes} 333