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

Annotation of src/usr.bin/wall/wall.c, Revision 1.15

1.15    ! millert     1: /*     $OpenBSD: wall.c,v 1.14 2001/02/13 09:16:04 deraadt Exp $       */
1.1       deraadt     2: /*     $NetBSD: wall.c,v 1.6 1994/11/17 07:17:58 jtc Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1988, 1990, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
1.15    ! millert    38: static const char copyright[] =
1.1       deraadt    39: "@(#) Copyright (c) 1988, 1990, 1993\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
1.15    ! millert    45: static const char sccsid[] = "@(#)wall.c       8.2 (Berkeley) 11/16/93";
1.1       deraadt    46: #endif
1.15    ! millert    47: static const char rcsid[] = "$OpenBSD: wall.c,v 1.14 2001/02/13 09:16:04 deraadt Exp $";
1.1       deraadt    48: #endif /* not lint */
                     49:
                     50: /*
                     51:  * This program is not related to David Wall, whose Stanford Ph.D. thesis
                     52:  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
                     53:  */
                     54:
                     55: #include <sys/param.h>
                     56: #include <sys/stat.h>
                     57: #include <sys/time.h>
                     58: #include <sys/uio.h>
                     59:
                     60: #include <paths.h>
                     61: #include <pwd.h>
1.6       deraadt    62: #include <grp.h>
1.1       deraadt    63: #include <stdio.h>
                     64: #include <stdlib.h>
                     65: #include <string.h>
                     66: #include <unistd.h>
                     67: #include <utmp.h>
1.4       deraadt    68: #include <vis.h>
1.9       form       69: #include <err.h>
1.1       deraadt    70:
1.6       deraadt    71: struct wallgroup {
1.15    ! millert    72:        gid_t   gid;
        !            73:        char    *name;
        !            74:        char    **mem;
1.6       deraadt    75:        struct wallgroup *next;
                     76: } *grouplist;
                     77:
1.15    ! millert    78: void   makemsg(char *);
        !            79: void   addgroup(struct group *, char *);
        !            80: char   *ttymsg(struct iovec *, int, char *, int);
        !            81: __dead void usage(void);
1.1       deraadt    82:
                     83: #define        IGNOREUSER      "sleeper"
                     84:
                     85: int nobanner;
                     86: int mbufsize;
                     87: char *mbuf;
                     88:
                     89: /* ARGSUSED */
                     90: int
1.15    ! millert    91: main(int argc, char **argv)
1.1       deraadt    92: {
1.15    ! millert    93:        FILE *fp;
        !            94:        int ch, ingroup;
1.1       deraadt    95:        struct iovec iov;
                     96:        struct utmp utmp;
1.15    ! millert    97:        char *p, **mem;
1.1       deraadt    98:        char line[sizeof(utmp.ut_line) + 1];
1.15    ! millert    99:        char username[sizeof(utmp.ut_name) + 1];
        !           100:        struct passwd *pw;
        !           101:        struct group *grp;
1.6       deraadt   102:        struct wallgroup *g;
1.1       deraadt   103:
1.8       millert   104:        while ((ch = getopt(argc, argv, "ng:")) != -1)
1.1       deraadt   105:                switch (ch) {
                    106:                case 'n':
                    107:                        /* undoc option for shutdown: suppress banner */
1.15    ! millert   108:                        pw = getpwnam("nobody");
        !           109:                        if (geteuid() == 0 || (pw && getuid() == pw->pw_uid))
1.1       deraadt   110:                                nobanner = 1;
                    111:                        break;
1.6       deraadt   112:                case 'g':
1.15    ! millert   113:                        grp = getgrnam(optarg);
        !           114:                        if ((grp = getgrnam(optarg)) == NULL)
        !           115:                                errx(1, "unknown group `%s'", optarg);
        !           116:                        addgroup(grp, optarg);
1.6       deraadt   117:                        break;
1.1       deraadt   118:                default:
1.15    ! millert   119:                        usage();
1.1       deraadt   120:                }
                    121:        argc -= optind;
                    122:        argv += optind;
                    123:        if (argc > 1)
1.15    ! millert   124:                usage();
1.6       deraadt   125:
1.1       deraadt   126:        makemsg(*argv);
                    127:
1.9       form      128:        if (!(fp = fopen(_PATH_UTMP, "r")))
1.13      deraadt   129:                errx(1, "cannot read %s.", _PATH_UTMP);
1.1       deraadt   130:        iov.iov_base = mbuf;
                    131:        iov.iov_len = mbufsize;
                    132:        /* NOSTRICT */
1.15    ! millert   133:        while (fread(&utmp, sizeof(utmp), 1, fp) == 1) {
1.1       deraadt   134:                if (!utmp.ut_name[0] ||
                    135:                    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
                    136:                        continue;
1.6       deraadt   137:                if (grouplist) {
1.15    ! millert   138:                        ingroup = 0;
        !           139:                        strncpy(username, utmp.ut_name, sizeof(utmp.ut_name));
        !           140:                        username[sizeof(utmp.ut_name)] = '\0';
1.6       deraadt   141:                        pw = getpwnam(username);
                    142:                        if (!pw)
                    143:                                continue;
                    144:                        for (g = grouplist; g && ingroup == 0; g = g->next) {
                    145:                                if (g->gid == pw->pw_gid)
                    146:                                        ingroup = 1;
1.15    ! millert   147:                                for (mem = g->mem; *mem && ingroup == 0; mem++)
        !           148:                                        if (strcmp(username, *mem) == 0)
1.6       deraadt   149:                                                ingroup = 1;
                    150:                        }
                    151:                        if (ingroup == 0)
                    152:                                continue;
                    153:                }
1.1       deraadt   154:                strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
                    155:                line[sizeof(utmp.ut_line)] = '\0';
                    156:                if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
1.13      deraadt   157:                        warnx("%s", p);
1.1       deraadt   158:        }
                    159:        exit(0);
                    160: }
                    161:
                    162: void
1.15    ! millert   163: makemsg(char *fname)
1.1       deraadt   164: {
1.15    ! millert   165:        int ch, cnt;
1.1       deraadt   166:        struct tm *lt;
                    167:        struct passwd *pw;
                    168:        struct stat sbuf;
                    169:        time_t now, time();
                    170:        FILE *fp;
                    171:        int fd;
1.10      deraadt   172:        char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[64];
1.4       deraadt   173:        char tmpbuf[5];
1.11      d         174:        char *ttynam;
1.1       deraadt   175:
1.10      deraadt   176:        snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
1.15    ! millert   177:        if ((fd = mkstemp(tmpname)) >= 0) {
        !           178:                (void)unlink(tmpname);
        !           179:                fp = fdopen(fd, "r+");
        !           180:        }
        !           181:        if (fd == -1 || fp == NULL)
1.13      deraadt   182:                errx(1, "can't open temporary file.");
1.1       deraadt   183:
                    184:        if (!nobanner) {
                    185:                if (!(whom = getlogin()))
                    186:                        whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
                    187:                (void)gethostname(hostname, sizeof(hostname));
                    188:                (void)time(&now);
                    189:                lt = localtime(&now);
1.11      d         190:                if ((ttynam = ttyname(STDERR_FILENO)) == NULL)
                    191:                        ttynam = "(not a tty)";
1.1       deraadt   192:
                    193:                /*
                    194:                 * all this stuff is to blank out a square for the message;
                    195:                 * we wrap message lines at column 79, not 80, because some
                    196:                 * terminals wrap after 79, some do not, and we can't tell.
                    197:                 * Which means that we may leave a non-blank character
                    198:                 * in column 80, but that can't be helped.
                    199:                 */
                    200:                (void)fprintf(fp, "\r%79s\r\n", " ");
1.3       deraadt   201:                (void)snprintf(lbuf, sizeof lbuf,
                    202:                    "Broadcast Message from %s@%s", whom, hostname);
1.1       deraadt   203:                (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
1.3       deraadt   204:                (void)snprintf(lbuf, sizeof lbuf,
1.11      d         205:                    "        (%s) at %d:%02d ...", ttynam,
1.1       deraadt   206:                    lt->tm_hour, lt->tm_min);
                    207:                (void)fprintf(fp, "%-79.79s\r\n", lbuf);
                    208:        }
                    209:        (void)fprintf(fp, "%79s\r\n", " ");
                    210:
1.13      deraadt   211:        if (fname) {
                    212:                gid_t egid = getegid();
                    213:
                    214:                setegid(getgid());
                    215:                if (freopen(fname, "r", stdin) == NULL)
                    216:                        errx(1, "can't read %s.", fname);
                    217:                setegid(egid);
                    218:        }
1.1       deraadt   219:        while (fgets(lbuf, sizeof(lbuf), stdin))
                    220:                for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
1.5       deraadt   221:                        vis(tmpbuf, ch, VIS_SAFE|VIS_NOSLASH, p[1]);
1.4       deraadt   222:                        if (cnt == 79+1-strlen(tmpbuf) || ch == '\n') {
                    223:                                for (; cnt < 79+1-strlen(tmpbuf); ++cnt)
1.1       deraadt   224:                                        putc(' ', fp);
                    225:                                putc('\r', fp);
                    226:                                putc('\n', fp);
                    227:                                cnt = -1;
                    228:                        }
1.4       deraadt   229:                        if (ch != '\n') {
                    230:                                int xx;
                    231:
                    232:                                for (xx = 0; tmpbuf[xx]; xx++)
                    233:                                        putc(tmpbuf[xx], fp);
                    234:                        }
1.1       deraadt   235:                }
                    236:        (void)fprintf(fp, "%79s\r\n", " ");
                    237:        rewind(fp);
                    238:
1.9       form      239:        if (fstat(fd, &sbuf))
1.13      deraadt   240:                errx(1, "can't stat temporary file.");
1.1       deraadt   241:        mbufsize = sbuf.st_size;
1.9       form      242:        if (!(mbuf = malloc((u_int)mbufsize)))
1.13      deraadt   243:                errx(1, "out of memory.");
1.9       form      244:        if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
1.13      deraadt   245:                errx(1, "can't read temporary file.");
1.1       deraadt   246:        (void)close(fd);
1.15    ! millert   247: }
        !           248:
        !           249: void
        !           250: addgroup(struct group *grp, char *name)
        !           251: {
        !           252:        int i;
        !           253:        struct wallgroup *g;
        !           254:
        !           255:        for (i = 0; grp->gr_mem[i]; i++)
        !           256:                ;
        !           257:
        !           258:        g = (struct wallgroup *)malloc(sizeof *g);
        !           259:        if (g == NULL)
        !           260:                errx(1, "out of memory.");
        !           261:        g->gid = grp->gr_gid;
        !           262:        g->name = name;
        !           263:        g->mem = (char **)malloc(i + 1);
        !           264:        if (g->mem == NULL)
        !           265:                errx(1, "out of memory.");
        !           266:        for (i = 0; grp->gr_mem[i] != NULL; i++) {
        !           267:                g->mem[i] = strdup(grp->gr_mem[i]);
        !           268:                if (g->mem[i] == NULL)
        !           269:                        errx(1, "out of memory.");
        !           270:        }
        !           271:        g->mem[i] = NULL;
        !           272:        g->next = grouplist;
        !           273:        grouplist = g;
        !           274: }
        !           275:
        !           276: void
        !           277: usage(void)
        !           278: {
        !           279:        extern char *__progname;
        !           280:
        !           281:        (void)fprintf(stderr, "usage: %s [-g group] [file]\n", __progname);
        !           282:        exit(1);
1.1       deraadt   283: }