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

1.32    ! bluhm       1: /*     $OpenBSD: write.c,v 1.31 2015/10/09 01:37:09 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.
1.20      millert    19:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #include <sys/stat.h>
                     37: #include <ctype.h>
                     38: #include <stdio.h>
1.22      david      39: #include <stdlib.h>
1.1       deraadt    40: #include <string.h>
                     41: #include <signal.h>
                     42: #include <time.h>
                     43: #include <fcntl.h>
1.7       downsj     44: #include <paths.h>
1.1       deraadt    45: #include <pwd.h>
                     46: #include <unistd.h>
1.28      deraadt    47: #include <limits.h>
1.1       deraadt    48: #include <utmp.h>
                     49: #include <err.h>
1.5       deraadt    50: #include <vis.h>
1.1       deraadt    51:
1.15      millert    52: void done(int sig);
                     53: void do_write(char *, char *, uid_t);
                     54: void wr_fputs(char *);
1.19      deraadt    55: void search_utmp(char *, char *, int, char *, uid_t);
1.15      millert    56: int term_chk(char *, int *, time_t *, int);
                     57: int utmp_chk(char *, char *);
1.1       deraadt    58:
                     59: int
1.21      deraadt    60: main(int argc, char *argv[])
1.1       deraadt    61: {
1.28      deraadt    62:        char tty[PATH_MAX], *mytty, *cp;
1.17      deraadt    63:        int msgsok, myttyfd;
1.1       deraadt    64:        time_t atime;
                     65:        uid_t myuid;
                     66:
                     67:        /* check that sender has write enabled */
                     68:        if (isatty(fileno(stdin)))
                     69:                myttyfd = fileno(stdin);
                     70:        else if (isatty(fileno(stdout)))
                     71:                myttyfd = fileno(stdout);
                     72:        else if (isatty(fileno(stderr)))
                     73:                myttyfd = fileno(stderr);
                     74:        else
                     75:                errx(1, "can't find your tty");
                     76:        if (!(mytty = ttyname(myttyfd)))
                     77:                errx(1, "can't find your tty's name");
1.12      jasoni     78:        if ((cp = strrchr(mytty, '/')))
1.1       deraadt    79:                mytty = cp + 1;
                     80:        if (term_chk(mytty, &msgsok, &atime, 1))
                     81:                exit(1);
                     82:        if (!msgsok)
1.2       deraadt    83:                warnx("you have write permission turned off");
1.1       deraadt    84:
                     85:        myuid = getuid();
                     86:
                     87:        /* check args */
                     88:        switch (argc) {
                     89:        case 2:
1.19      deraadt    90:                search_utmp(argv[1], tty, sizeof tty, mytty, myuid);
1.1       deraadt    91:                do_write(tty, mytty, myuid);
                     92:                break;
                     93:        case 3:
1.16      fgsch      94:                if (!strncmp(argv[2], _PATH_DEV, sizeof(_PATH_DEV) - 1))
                     95:                        argv[2] += sizeof(_PATH_DEV) - 1;
1.1       deraadt    96:                if (utmp_chk(argv[1], argv[2]))
                     97:                        errx(1, "%s is not logged in on %s",
                     98:                            argv[1], argv[2]);
                     99:                if (term_chk(argv[2], &msgsok, &atime, 1))
                    100:                        exit(1);
                    101:                if (myuid && !msgsok)
                    102:                        errx(1, "%s has messages disabled on %s",
                    103:                            argv[1], argv[2]);
                    104:                do_write(argv[2], mytty, myuid);
                    105:                break;
                    106:        default:
1.25      sobrado   107:                (void)fprintf(stderr, "usage: write user [ttyname]\n");
1.1       deraadt   108:                exit(1);
                    109:        }
1.11      deraadt   110:        done(0);
1.12      jasoni    111:
1.1       deraadt   112:        /* NOTREACHED */
1.12      jasoni    113:        return (0);
1.1       deraadt   114: }
                    115:
                    116: /*
                    117:  * utmp_chk - checks that the given user is actually logged in on
                    118:  *     the given tty
                    119:  */
                    120: int
1.17      deraadt   121: utmp_chk(char *user, char *tty)
1.1       deraadt   122: {
                    123:        struct utmp u;
                    124:        int ufd;
                    125:
                    126:        if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
1.24      deraadt   127:                return(1);      /* no utmp, cannot talk to users */
1.1       deraadt   128:
                    129:        while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
                    130:                if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
                    131:                    strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
                    132:                        (void)close(ufd);
                    133:                        return(0);
                    134:                }
                    135:
                    136:        (void)close(ufd);
                    137:        return(1);
                    138: }
                    139:
                    140: /*
                    141:  * search_utmp - search utmp for the "best" terminal to write to
                    142:  *
                    143:  * Ignores terminals with messages disabled, and of the rest, returns
                    144:  * the one with the most recent access time.  Returns as value the number
                    145:  * of the user's terminals with messages enabled, or -1 if the user is
                    146:  * not logged in at all.
                    147:  *
                    148:  * Special case for writing to yourself - ignore the terminal you're
                    149:  * writing from, unless that's the only terminal with messages enabled.
                    150:  */
                    151: void
1.19      deraadt   152: search_utmp(char *user, char *tty, int ttyl, char *mytty, uid_t myuid)
1.1       deraadt   153: {
                    154:        struct utmp u;
                    155:        time_t bestatime, atime;
                    156:        int ufd, nloggedttys, nttys, msgsok, user_is_me;
                    157:        char atty[UT_LINESIZE + 1];
                    158:
                    159:        if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0)
                    160:                err(1, "%s", _PATH_UTMP);
                    161:
                    162:        nloggedttys = nttys = 0;
                    163:        bestatime = 0;
                    164:        user_is_me = 0;
                    165:        while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u))
                    166:                if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
                    167:                        ++nloggedttys;
                    168:                        (void)strncpy(atty, u.ut_line, UT_LINESIZE);
                    169:                        atty[UT_LINESIZE] = '\0';
                    170:                        if (term_chk(atty, &msgsok, &atime, 0))
                    171:                                continue;       /* bad term? skip */
                    172:                        if (myuid && !msgsok)
                    173:                                continue;       /* skip ttys with msgs off */
                    174:                        if (strcmp(atty, mytty) == 0) {
                    175:                                user_is_me = 1;
                    176:                                continue;       /* don't write to yourself */
                    177:                        }
                    178:                        ++nttys;
                    179:                        if (atime > bestatime) {
                    180:                                bestatime = atime;
1.19      deraadt   181:                                (void)strlcpy(tty, atty, ttyl);
1.1       deraadt   182:                        }
                    183:                }
                    184:
                    185:        (void)close(ufd);
                    186:        if (nloggedttys == 0)
                    187:                errx(1, "%s is not logged in", user);
                    188:        if (nttys == 0) {
                    189:                if (user_is_me) {               /* ok, so write to yourself! */
1.19      deraadt   190:                        (void)strlcpy(tty, mytty, ttyl);
1.1       deraadt   191:                        return;
                    192:                }
                    193:                errx(1, "%s has messages disabled", user);
                    194:        } else if (nttys > 1)
                    195:                warnx("%s is logged in more than once; writing to %s",
                    196:                    user, tty);
                    197: }
                    198:
                    199: /*
                    200:  * term_chk - check that a terminal exists, and get the message bit
                    201:  *     and the access time
                    202:  */
                    203: int
1.17      deraadt   204: term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
1.1       deraadt   205: {
                    206:        struct stat s;
1.28      deraadt   207:        char path[PATH_MAX];
1.1       deraadt   208:
1.7       downsj    209:        (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
1.1       deraadt   210:        if (stat(path, &s) < 0) {
                    211:                if (showerror)
                    212:                        warn("%s", path);
                    213:                return(1);
                    214:        }
1.9       deraadt   215:        *msgsokP = (s.st_mode & S_IWGRP) != 0;  /* group write bit */
1.1       deraadt   216:        *atimeP = s.st_atime;
                    217:        return(0);
                    218: }
                    219:
                    220: /*
                    221:  * do_write - actually make the connection
                    222:  */
                    223: void
1.17      deraadt   224: do_write(char *tty, char *mytty, uid_t myuid)
1.1       deraadt   225: {
1.14      mpech     226:        char *login, *nows;
                    227:        struct passwd *pwd;
1.1       deraadt   228:        time_t now;
1.28      deraadt   229:        char path[PATH_MAX], host[HOST_NAME_MAX+1], line[512];
1.23      djm       230:        gid_t gid;
1.32    ! bluhm     231:        int fd;
1.1       deraadt   232:
                    233:        /* Determine our login name before the we reopen() stdout */
1.12      jasoni    234:        if ((login = getlogin()) == NULL) {
                    235:                if ((pwd = getpwuid(myuid)))
1.1       deraadt   236:                        login = pwd->pw_name;
                    237:                else
                    238:                        login = "???";
1.12      jasoni    239:        }
1.1       deraadt   240:
1.7       downsj    241:        (void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
1.32    ! bluhm     242:        fd = open(path, O_WRONLY, 0666);
        !           243:        if (fd == -1)
        !           244:                err(1, "open %s", path);
        !           245:        fflush(stdout);
        !           246:        if (dup2(fd, STDOUT_FILENO) == -1)
        !           247:                err(1, "dup2 %s", path);
        !           248:        if (fd != STDOUT_FILENO)
        !           249:                close(fd);
1.1       deraadt   250:
1.17      deraadt   251:        /* revoke privs, now that we have opened the tty */
1.23      djm       252:        gid = getgid();
                    253:        if (setresgid(gid, gid, gid) == -1)
                    254:                err(1, "setresgid");
1.30      deraadt   255:
                    256:        /*
                    257:         * Unfortunately this is rather late - well after utmp
                    258:         * parsing, then pinned by the tty open and setresgid
                    259:         */
1.31      deraadt   260:        if (pledge("stdio", NULL) == -1)
                    261:                err(1, "pledge");
1.17      deraadt   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)
1.19      deraadt   268:                (void)strlcpy(host, "???", sizeof host);
1.27      deraadt   269:        now = time(NULL);
1.1       deraadt   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: }