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

1.14    ! deraadt     1: /*     $OpenBSD: wall.c,v 1.13 2000/09/07 17:23:26 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
                     38: static char copyright[] =
                     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
                     45: static char sccsid[] = "@(#)wall.c     8.2 (Berkeley) 11/16/93";
                     46: #endif
1.14    ! deraadt    47: static char rcsid[] = "$OpenBSD: wall.c,v 1.13 2000/09/07 17:23:26 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 {
                     72:        struct wallgroup *next;
                     73:        char    *name;
                     74:        gid_t   gid;
                     75: } *grouplist;
                     76:
1.1       deraadt    77: void   makemsg __P((char *));
                     78:
                     79: #define        IGNOREUSER      "sleeper"
                     80:
                     81: int nobanner;
                     82: int mbufsize;
                     83: char *mbuf;
                     84:
                     85: /* ARGSUSED */
                     86: int
                     87: main(argc, argv)
                     88:        int argc;
                     89:        char **argv;
                     90: {
                     91:        extern int optind;
                     92:        int ch;
                     93:        struct iovec iov;
                     94:        struct utmp utmp;
                     95:        FILE *fp;
                     96:        char *p, *ttymsg();
                     97:        struct passwd *pep = getpwnam("nobody");
                     98:        char line[sizeof(utmp.ut_line) + 1];
1.6       deraadt    99:        struct wallgroup *g;
1.1       deraadt   100:
1.8       millert   101:        while ((ch = getopt(argc, argv, "ng:")) != -1)
1.1       deraadt   102:                switch (ch) {
                    103:                case 'n':
                    104:                        /* undoc option for shutdown: suppress banner */
                    105:                        if (geteuid() == 0 || (pep && getuid() == pep->pw_uid))
                    106:                                nobanner = 1;
                    107:                        break;
1.6       deraadt   108:                case 'g':
                    109:                        g = (struct wallgroup *)malloc(sizeof *g);
                    110:                        g->next = grouplist;
                    111:                        g->name = optarg;
                    112:                        g->gid = -1;
                    113:                        grouplist = g;
                    114:                        break;
1.1       deraadt   115:                case '?':
                    116:                default:
                    117: usage:
1.6       deraadt   118:                        (void)fprintf(stderr, "usage: wall [-g group] [file]\n");
1.1       deraadt   119:                        exit(1);
                    120:                }
                    121:        argc -= optind;
                    122:        argv += optind;
                    123:        if (argc > 1)
                    124:                goto usage;
                    125:
1.6       deraadt   126:        for (g = grouplist; g; g = g->next) {
                    127:                struct group *grp;
                    128:
                    129:                grp = getgrnam(g->name);
                    130:                if (grp)
                    131:                        g->gid = grp->gr_gid;
                    132:        }
                    133:
1.1       deraadt   134:        makemsg(*argv);
                    135:
1.9       form      136:        if (!(fp = fopen(_PATH_UTMP, "r")))
1.13      deraadt   137:                errx(1, "cannot read %s.", _PATH_UTMP);
1.1       deraadt   138:        iov.iov_base = mbuf;
                    139:        iov.iov_len = mbufsize;
                    140:        /* NOSTRICT */
                    141:        while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
                    142:                if (!utmp.ut_name[0] ||
                    143:                    !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
                    144:                        continue;
1.6       deraadt   145:                if (grouplist) {
                    146:                        int ingroup = 0, ngrps, i;
1.14    ! deraadt   147:                        char username[MAXLOGNAME];
1.6       deraadt   148:                        struct passwd *pw;
                    149:                        gid_t grps[NGROUPS_MAX];
                    150:
                    151:                        bzero(username, sizeof username);
                    152:                        strncpy(username, utmp.ut_name, sizeof utmp.ut_name);
                    153:                        pw = getpwnam(username);
                    154:                        if (!pw)
                    155:                                continue;
                    156:                        ngrps = getgroups(pw->pw_gid, grps);
                    157:                        for (g = grouplist; g && ingroup == 0; g = g->next) {
                    158:                                if (g->gid == -1)
                    159:                                        continue;
                    160:                                if (g->gid == pw->pw_gid)
                    161:                                        ingroup = 1;
                    162:                                for (i = 0; i < ngrps && ingroup == 0; i++)
                    163:                                        if (g->gid == grps[i])
                    164:                                                ingroup = 1;
                    165:                        }
                    166:                        if (ingroup == 0)
                    167:                                continue;
                    168:                }
1.1       deraadt   169:                strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
                    170:                line[sizeof(utmp.ut_line)] = '\0';
                    171:                if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
1.13      deraadt   172:                        warnx("%s", p);
1.1       deraadt   173:        }
                    174:        exit(0);
                    175: }
                    176:
                    177: void
                    178: makemsg(fname)
                    179:        char *fname;
                    180: {
                    181:        register int ch, cnt;
                    182:        struct tm *lt;
                    183:        struct passwd *pw;
                    184:        struct stat sbuf;
                    185:        time_t now, time();
                    186:        FILE *fp;
                    187:        int fd;
1.10      deraadt   188:        char *p, *whom, hostname[MAXHOSTNAMELEN], lbuf[100], tmpname[64];
1.4       deraadt   189:        char tmpbuf[5];
1.11      d         190:        char *ttynam;
1.1       deraadt   191:
1.10      deraadt   192:        snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
1.12      deraadt   193:        if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
1.13      deraadt   194:                errx(1, "can't open temporary file.");
1.1       deraadt   195:        (void)unlink(tmpname);
                    196:
                    197:        if (!nobanner) {
                    198:                if (!(whom = getlogin()))
                    199:                        whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
                    200:                (void)gethostname(hostname, sizeof(hostname));
                    201:                (void)time(&now);
                    202:                lt = localtime(&now);
1.11      d         203:                if ((ttynam = ttyname(STDERR_FILENO)) == NULL)
                    204:                        ttynam = "(not a tty)";
1.1       deraadt   205:
                    206:                /*
                    207:                 * all this stuff is to blank out a square for the message;
                    208:                 * we wrap message lines at column 79, not 80, because some
                    209:                 * terminals wrap after 79, some do not, and we can't tell.
                    210:                 * Which means that we may leave a non-blank character
                    211:                 * in column 80, but that can't be helped.
                    212:                 */
                    213:                (void)fprintf(fp, "\r%79s\r\n", " ");
1.3       deraadt   214:                (void)snprintf(lbuf, sizeof lbuf,
                    215:                    "Broadcast Message from %s@%s", whom, hostname);
1.1       deraadt   216:                (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
1.3       deraadt   217:                (void)snprintf(lbuf, sizeof lbuf,
1.11      d         218:                    "        (%s) at %d:%02d ...", ttynam,
1.1       deraadt   219:                    lt->tm_hour, lt->tm_min);
                    220:                (void)fprintf(fp, "%-79.79s\r\n", lbuf);
                    221:        }
                    222:        (void)fprintf(fp, "%79s\r\n", " ");
                    223:
1.13      deraadt   224:        if (fname) {
                    225:                gid_t egid = getegid();
                    226:
                    227:                setegid(getgid());
                    228:                if (freopen(fname, "r", stdin) == NULL)
                    229:                        errx(1, "can't read %s.", fname);
                    230:                setegid(egid);
                    231:        }
1.1       deraadt   232:        while (fgets(lbuf, sizeof(lbuf), stdin))
                    233:                for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
1.5       deraadt   234:                        vis(tmpbuf, ch, VIS_SAFE|VIS_NOSLASH, p[1]);
1.4       deraadt   235:                        if (cnt == 79+1-strlen(tmpbuf) || ch == '\n') {
                    236:                                for (; cnt < 79+1-strlen(tmpbuf); ++cnt)
1.1       deraadt   237:                                        putc(' ', fp);
                    238:                                putc('\r', fp);
                    239:                                putc('\n', fp);
                    240:                                cnt = -1;
                    241:                        }
1.4       deraadt   242:                        if (ch != '\n') {
                    243:                                int xx;
                    244:
                    245:                                for (xx = 0; tmpbuf[xx]; xx++)
                    246:                                        putc(tmpbuf[xx], fp);
                    247:                        }
1.1       deraadt   248:                }
                    249:        (void)fprintf(fp, "%79s\r\n", " ");
                    250:        rewind(fp);
                    251:
1.9       form      252:        if (fstat(fd, &sbuf))
1.13      deraadt   253:                errx(1, "can't stat temporary file.");
1.1       deraadt   254:        mbufsize = sbuf.st_size;
1.9       form      255:        if (!(mbuf = malloc((u_int)mbufsize)))
1.13      deraadt   256:                errx(1, "out of memory.");
1.9       form      257:        if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
1.13      deraadt   258:                errx(1, "can't read temporary file.");
1.1       deraadt   259:        (void)close(fd);
                    260: }