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

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