write.c revision 19190
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 381590Srgrimesstatic 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 441590Srgrimesstatic char sccsid[] = "@(#)write.c 8.1 (Berkeley) 6/6/93"; 451590Srgrimes#endif /* not lint */ 461590Srgrimes 471590Srgrimes#include <sys/param.h> 481590Srgrimes#include <sys/signal.h> 491590Srgrimes#include <sys/stat.h> 501590Srgrimes#include <sys/file.h> 511590Srgrimes#include <sys/time.h> 521590Srgrimes#include <utmp.h> 531590Srgrimes#include <ctype.h> 5419190Salex#include <paths.h> 551590Srgrimes#include <pwd.h> 561590Srgrimes#include <stdio.h> 571590Srgrimes#include <string.h> 581590Srgrimes 591590Srgrimesextern int errno; 601590Srgrimes 611590Srgrimesmain(argc, argv) 621590Srgrimes int argc; 631590Srgrimes char **argv; 641590Srgrimes{ 651590Srgrimes register char *cp; 661590Srgrimes time_t atime; 671590Srgrimes uid_t myuid; 681590Srgrimes int msgsok, myttyfd; 691590Srgrimes char tty[MAXPATHLEN], *mytty, *ttyname(); 701590Srgrimes void done(); 711590Srgrimes 721590Srgrimes /* check that sender has write enabled */ 731590Srgrimes if (isatty(fileno(stdin))) 741590Srgrimes myttyfd = fileno(stdin); 751590Srgrimes else if (isatty(fileno(stdout))) 761590Srgrimes myttyfd = fileno(stdout); 771590Srgrimes else if (isatty(fileno(stderr))) 781590Srgrimes myttyfd = fileno(stderr); 791590Srgrimes else { 801590Srgrimes (void)fprintf(stderr, "write: can't find your tty\n"); 811590Srgrimes exit(1); 821590Srgrimes } 831590Srgrimes if (!(mytty = ttyname(myttyfd))) { 841590Srgrimes (void)fprintf(stderr, "write: can't find your tty's name\n"); 851590Srgrimes exit(1); 861590Srgrimes } 871590Srgrimes if (cp = rindex(mytty, '/')) 881590Srgrimes mytty = cp + 1; 891590Srgrimes if (term_chk(mytty, &msgsok, &atime, 1)) 901590Srgrimes exit(1); 911590Srgrimes if (!msgsok) { 921590Srgrimes (void)fprintf(stderr, 931590Srgrimes "write: you have write permission turned off.\n"); 941590Srgrimes exit(1); 951590Srgrimes } 961590Srgrimes 971590Srgrimes myuid = getuid(); 981590Srgrimes 991590Srgrimes /* check args */ 1001590Srgrimes switch (argc) { 1011590Srgrimes case 2: 1021590Srgrimes search_utmp(argv[1], tty, mytty, myuid); 1031590Srgrimes do_write(tty, mytty, myuid); 1041590Srgrimes break; 1051590Srgrimes case 3: 10619190Salex if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV))) 1071590Srgrimes argv[2] += 5; 1081590Srgrimes if (utmp_chk(argv[1], argv[2])) { 1091590Srgrimes (void)fprintf(stderr, 1101590Srgrimes "write: %s is not logged in on %s.\n", 1111590Srgrimes argv[1], argv[2]); 1121590Srgrimes exit(1); 1131590Srgrimes } 1141590Srgrimes if (term_chk(argv[2], &msgsok, &atime, 1)) 1151590Srgrimes exit(1); 1161590Srgrimes if (myuid && !msgsok) { 1171590Srgrimes (void)fprintf(stderr, 1181590Srgrimes "write: %s has messages disabled on %s\n", 1191590Srgrimes argv[1], argv[2]); 1201590Srgrimes exit(1); 1211590Srgrimes } 1221590Srgrimes do_write(argv[2], mytty, myuid); 1231590Srgrimes break; 1241590Srgrimes default: 1251590Srgrimes (void)fprintf(stderr, "usage: write user [tty]\n"); 1261590Srgrimes exit(1); 1271590Srgrimes } 1281590Srgrimes done(); 1291590Srgrimes /* NOTREACHED */ 1301590Srgrimes} 1311590Srgrimes 1321590Srgrimes/* 1331590Srgrimes * utmp_chk - checks that the given user is actually logged in on 1341590Srgrimes * the given tty 1351590Srgrimes */ 1361590Srgrimesutmp_chk(user, tty) 1371590Srgrimes char *user, *tty; 1381590Srgrimes{ 1391590Srgrimes struct utmp u; 1401590Srgrimes int ufd; 1411590Srgrimes 1421590Srgrimes if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) 1431590Srgrimes return(0); /* ignore error, shouldn't happen anyway */ 1441590Srgrimes 1451590Srgrimes while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 1461590Srgrimes if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 && 1471590Srgrimes strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) { 1481590Srgrimes (void)close(ufd); 1491590Srgrimes return(0); 1501590Srgrimes } 1511590Srgrimes 1521590Srgrimes (void)close(ufd); 1531590Srgrimes return(1); 1541590Srgrimes} 1551590Srgrimes 1561590Srgrimes/* 1571590Srgrimes * search_utmp - search utmp for the "best" terminal to write to 1581590Srgrimes * 1591590Srgrimes * Ignores terminals with messages disabled, and of the rest, returns 1601590Srgrimes * the one with the most recent access time. Returns as value the number 1611590Srgrimes * of the user's terminals with messages enabled, or -1 if the user is 1621590Srgrimes * not logged in at all. 1631590Srgrimes * 1641590Srgrimes * Special case for writing to yourself - ignore the terminal you're 1651590Srgrimes * writing from, unless that's the only terminal with messages enabled. 1661590Srgrimes */ 1671590Srgrimessearch_utmp(user, tty, mytty, myuid) 1681590Srgrimes char *user, *tty, *mytty; 1691590Srgrimes uid_t myuid; 1701590Srgrimes{ 1711590Srgrimes struct utmp u; 1721590Srgrimes time_t bestatime, atime; 1731590Srgrimes int ufd, nloggedttys, nttys, msgsok, user_is_me; 1741590Srgrimes char atty[UT_LINESIZE + 1]; 1751590Srgrimes 1761590Srgrimes if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) { 1771590Srgrimes perror("utmp"); 1781590Srgrimes exit(1); 1791590Srgrimes } 1801590Srgrimes 1811590Srgrimes nloggedttys = nttys = 0; 1821590Srgrimes bestatime = 0; 1831590Srgrimes user_is_me = 0; 1841590Srgrimes while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) 1851590Srgrimes if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) { 1861590Srgrimes ++nloggedttys; 1871590Srgrimes (void)strncpy(atty, u.ut_line, UT_LINESIZE); 1881590Srgrimes atty[UT_LINESIZE] = '\0'; 1891590Srgrimes if (term_chk(atty, &msgsok, &atime, 0)) 1901590Srgrimes continue; /* bad term? skip */ 1911590Srgrimes if (myuid && !msgsok) 1921590Srgrimes continue; /* skip ttys with msgs off */ 1931590Srgrimes if (strcmp(atty, mytty) == 0) { 1941590Srgrimes user_is_me = 1; 1951590Srgrimes continue; /* don't write to yourself */ 1961590Srgrimes } 1971590Srgrimes ++nttys; 1981590Srgrimes if (atime > bestatime) { 1991590Srgrimes bestatime = atime; 2001590Srgrimes (void)strcpy(tty, atty); 2011590Srgrimes } 2021590Srgrimes } 2031590Srgrimes 2041590Srgrimes (void)close(ufd); 2051590Srgrimes if (nloggedttys == 0) { 2061590Srgrimes (void)fprintf(stderr, "write: %s is not logged in\n", user); 2071590Srgrimes exit(1); 2081590Srgrimes } 2091590Srgrimes if (nttys == 0) { 2101590Srgrimes if (user_is_me) { /* ok, so write to yourself! */ 2111590Srgrimes (void)strcpy(tty, mytty); 2121590Srgrimes return; 2131590Srgrimes } 2141590Srgrimes (void)fprintf(stderr, 2151590Srgrimes "write: %s has messages disabled\n", user); 2161590Srgrimes exit(1); 2171590Srgrimes } else if (nttys > 1) { 2181590Srgrimes (void)fprintf(stderr, 2191590Srgrimes "write: %s is logged in more than once; writing to %s\n", 2201590Srgrimes 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 */ 2281590Srgrimesterm_chk(tty, msgsokP, atimeP, showerror) 2291590Srgrimes char *tty; 2301590Srgrimes int *msgsokP, showerror; 2311590Srgrimes time_t *atimeP; 2321590Srgrimes{ 2331590Srgrimes struct stat s; 2341590Srgrimes char path[MAXPATHLEN]; 2351590Srgrimes 23619190Salex (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty); 2371590Srgrimes if (stat(path, &s) < 0) { 2381590Srgrimes if (showerror) 2391590Srgrimes (void)fprintf(stderr, 2401590Srgrimes "write: %s: %s\n", path, strerror(errno)); 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 */ 2511590Srgrimesdo_write(tty, mytty, myuid) 2521590Srgrimes char *tty, *mytty; 2531590Srgrimes uid_t myuid; 2541590Srgrimes{ 2551590Srgrimes register char *login, *nows; 2561590Srgrimes register struct passwd *pwd; 2571590Srgrimes time_t now, time(); 2581590Srgrimes char *getlogin(), path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512]; 2591590Srgrimes void done(); 2601590Srgrimes 2611590Srgrimes /* Determine our login name before the we reopen() stdout */ 2621590Srgrimes if ((login = getlogin()) == NULL) 2631590Srgrimes 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); 2691590Srgrimes if ((freopen(path, "w", stdout)) == NULL) { 2701590Srgrimes (void)fprintf(stderr, "write: %s: %s\n", path, strerror(errno)); 2711590Srgrimes exit(1); 2721590Srgrimes } 2731590Srgrimes 2741590Srgrimes (void)signal(SIGINT, done); 2751590Srgrimes (void)signal(SIGHUP, done); 2761590Srgrimes 2771590Srgrimes /* print greeting */ 2781590Srgrimes if (gethostname(host, sizeof(host)) < 0) 2791590Srgrimes (void)strcpy(host, "???"); 2801590Srgrimes now = time((time_t *)NULL); 2811590Srgrimes nows = ctime(&now); 2821590Srgrimes nows[16] = '\0'; 2831590Srgrimes (void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n", 2841590Srgrimes login, host, mytty, nows + 11); 2851590Srgrimes 2861590Srgrimes while (fgets(line, sizeof(line), stdin) != NULL) 2871590Srgrimes wr_fputs(line); 2881590Srgrimes} 2891590Srgrimes 2901590Srgrimes/* 2911590Srgrimes * done - cleanup and exit 2921590Srgrimes */ 2931590Srgrimesvoid 2941590Srgrimesdone() 2951590Srgrimes{ 2961590Srgrimes (void)printf("EOF\r\n"); 2971590Srgrimes exit(0); 2981590Srgrimes} 2991590Srgrimes 3001590Srgrimes/* 3011590Srgrimes * wr_fputs - like fputs(), but makes control characters visible and 3021590Srgrimes * turns \n into \r\n 3031590Srgrimes */ 3041590Srgrimeswr_fputs(s) 30512097Sache register unsigned char *s; 3061590Srgrimes{ 3071590Srgrimes 3081590Srgrimes#define PUTC(c) if (putchar(c) == EOF) goto err; 3091590Srgrimes 3101590Srgrimes for (; *s != '\0'; ++s) { 31111916Sache if (*s == '\n') { 3121590Srgrimes PUTC('\r'); 31311916Sache } else if (!isprint(*s) && !isspace(*s) && *s != '\007') { 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 3281590Srgrimeserr: (void)fprintf(stderr, "write: %s\n", strerror(errno)); 3291590Srgrimes exit(1); 3301590Srgrimes#undef PUTC 3311590Srgrimes} 332