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

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