[BACK]Return to write.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / write

Annotation of src/usr.bin/write/write.c, Revision 1.8

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