[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.18

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