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

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