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

1.29    ! benno       1: /*     $OpenBSD: wall.c,v 1.28 2015/09/29 03:19:24 guenther 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: /*
                     34:  * This program is not related to David Wall, whose Stanford Ph.D. thesis
                     35:  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
                     36:  */
                     37:
1.29    ! benno      38: #include <sys/queue.h>
1.1       deraadt    39: #include <sys/stat.h>
                     40: #include <sys/time.h>
                     41: #include <sys/uio.h>
                     42:
                     43: #include <paths.h>
                     44: #include <pwd.h>
1.6       deraadt    45: #include <grp.h>
1.1       deraadt    46: #include <stdio.h>
                     47: #include <stdlib.h>
                     48: #include <string.h>
                     49: #include <unistd.h>
1.26      deraadt    50: #include <limits.h>
1.1       deraadt    51: #include <utmp.h>
1.4       deraadt    52: #include <vis.h>
1.9       form       53: #include <err.h>
1.1       deraadt    54:
1.6       deraadt    55: struct wallgroup {
1.15      millert    56:        gid_t   gid;
                     57:        char    *name;
                     58:        char    **mem;
1.6       deraadt    59:        struct wallgroup *next;
                     60: } *grouplist;
                     61:
1.29    ! benno      62: struct utmptty {
        !            63:        char    *tty;
        !            64:        SLIST_ENTRY(utmptty) next;
        !            65: };
        !            66:
1.15      millert    67: void   makemsg(char *);
                     68: void   addgroup(struct group *, char *);
                     69: char   *ttymsg(struct iovec *, int, char *, int);
                     70: __dead void usage(void);
1.1       deraadt    71:
                     72: int nobanner;
                     73: int mbufsize;
                     74: char *mbuf;
                     75:
                     76: /* ARGSUSED */
                     77: int
1.15      millert    78: main(int argc, char **argv)
1.1       deraadt    79: {
1.15      millert    80:        FILE *fp;
                     81:        int ch, ingroup;
1.1       deraadt    82:        struct iovec iov;
                     83:        struct utmp utmp;
1.15      millert    84:        char *p, **mem;
1.1       deraadt    85:        char line[sizeof(utmp.ut_line) + 1];
1.15      millert    86:        char username[sizeof(utmp.ut_name) + 1];
                     87:        struct passwd *pw;
                     88:        struct group *grp;
1.6       deraadt    89:        struct wallgroup *g;
1.29    ! benno      90:        struct utmptty *un;
        !            91:        SLIST_HEAD(,utmptty) utmphead;
        !            92:        SLIST_INIT(&utmphead);
1.1       deraadt    93:
1.8       millert    94:        while ((ch = getopt(argc, argv, "ng:")) != -1)
1.1       deraadt    95:                switch (ch) {
                     96:                case 'n':
                     97:                        /* undoc option for shutdown: suppress banner */
1.15      millert    98:                        pw = getpwnam("nobody");
                     99:                        if (geteuid() == 0 || (pw && getuid() == pw->pw_uid))
1.1       deraadt   100:                                nobanner = 1;
                    101:                        break;
1.6       deraadt   102:                case 'g':
1.15      millert   103:                        if ((grp = getgrnam(optarg)) == NULL)
                    104:                                errx(1, "unknown group `%s'", optarg);
                    105:                        addgroup(grp, optarg);
1.6       deraadt   106:                        break;
1.1       deraadt   107:                default:
1.15      millert   108:                        usage();
1.1       deraadt   109:                }
                    110:        argc -= optind;
                    111:        argv += optind;
                    112:        if (argc > 1)
1.15      millert   113:                usage();
1.6       deraadt   114:
1.1       deraadt   115:        makemsg(*argv);
                    116:
1.29    ! benno     117:        if (pledge("stdio rpath wpath getpw proc", NULL) == -1)
        !           118:                err(1, "pledge");
        !           119:
1.9       form      120:        if (!(fp = fopen(_PATH_UTMP, "r")))
1.21      henning   121:                err(1, "cannot read %s", _PATH_UTMP);
1.1       deraadt   122:        iov.iov_base = mbuf;
                    123:        iov.iov_len = mbufsize;
1.29    ! benno     124:
1.15      millert   125:        while (fread(&utmp, sizeof(utmp), 1, fp) == 1) {
1.16      deraadt   126:                if (!utmp.ut_name[0])
1.1       deraadt   127:                        continue;
1.6       deraadt   128:                if (grouplist) {
1.15      millert   129:                        ingroup = 0;
                    130:                        strncpy(username, utmp.ut_name, sizeof(utmp.ut_name));
                    131:                        username[sizeof(utmp.ut_name)] = '\0';
1.6       deraadt   132:                        pw = getpwnam(username);
                    133:                        if (!pw)
                    134:                                continue;
                    135:                        for (g = grouplist; g && ingroup == 0; g = g->next) {
                    136:                                if (g->gid == pw->pw_gid)
                    137:                                        ingroup = 1;
1.15      millert   138:                                for (mem = g->mem; *mem && ingroup == 0; mem++)
                    139:                                        if (strcmp(username, *mem) == 0)
1.6       deraadt   140:                                                ingroup = 1;
                    141:                        }
                    142:                        if (ingroup == 0)
                    143:                                continue;
                    144:                }
1.1       deraadt   145:                strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
                    146:                line[sizeof(utmp.ut_line)] = '\0';
1.29    ! benno     147:                un = malloc(sizeof(struct utmptty));
        !           148:                if (un == NULL)
        !           149:                        err(1, "malloc");
        !           150:                un->tty = strndup(line, sizeof(utmp.ut_line));
        !           151:                if (un->tty == NULL)
        !           152:                        err(1, "strndup");
        !           153:                SLIST_INSERT_HEAD(&utmphead, un, next);
        !           154:        }
        !           155:        fclose(fp);
        !           156:
        !           157:        if (pledge("stdio rpath wpath proc", NULL) == -1)
        !           158:                err(1, "pledge");
        !           159:
        !           160:        SLIST_FOREACH(un, &utmphead, next) {
        !           161:                if ((p = ttymsg(&iov, 1, un->tty, 60*5)) != NULL)
1.13      deraadt   162:                        warnx("%s", p);
1.1       deraadt   163:        }
1.29    ! benno     164:
1.1       deraadt   165:        exit(0);
                    166: }
                    167:
                    168: void
1.15      millert   169: makemsg(char *fname)
1.1       deraadt   170: {
1.15      millert   171:        int ch, cnt;
1.1       deraadt   172:        struct tm *lt;
                    173:        struct passwd *pw;
                    174:        struct stat sbuf;
1.19      deraadt   175:        time_t now;
1.1       deraadt   176:        FILE *fp;
                    177:        int fd;
1.26      deraadt   178:        char *p, *whom, hostname[HOST_NAME_MAX+1], lbuf[100], tmpname[PATH_MAX];
1.4       deraadt   179:        char tmpbuf[5];
1.11      d         180:        char *ttynam;
1.1       deraadt   181:
1.20      avsm      182:        snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXXXXXX", _PATH_TMP);
1.15      millert   183:        if ((fd = mkstemp(tmpname)) >= 0) {
                    184:                (void)unlink(tmpname);
                    185:                fp = fdopen(fd, "r+");
                    186:        }
                    187:        if (fd == -1 || fp == NULL)
1.21      henning   188:                err(1, "can't open temporary file");
1.1       deraadt   189:
                    190:        if (!nobanner) {
                    191:                if (!(whom = getlogin()))
                    192:                        whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
                    193:                (void)gethostname(hostname, sizeof(hostname));
                    194:                (void)time(&now);
                    195:                lt = localtime(&now);
1.11      d         196:                if ((ttynam = ttyname(STDERR_FILENO)) == NULL)
                    197:                        ttynam = "(not a tty)";
1.1       deraadt   198:
                    199:                /*
                    200:                 * all this stuff is to blank out a square for the message;
                    201:                 * we wrap message lines at column 79, not 80, because some
                    202:                 * terminals wrap after 79, some do not, and we can't tell.
                    203:                 * Which means that we may leave a non-blank character
                    204:                 * in column 80, but that can't be helped.
                    205:                 */
                    206:                (void)fprintf(fp, "\r%79s\r\n", " ");
1.3       deraadt   207:                (void)snprintf(lbuf, sizeof lbuf,
                    208:                    "Broadcast Message from %s@%s", whom, hostname);
1.1       deraadt   209:                (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
1.3       deraadt   210:                (void)snprintf(lbuf, sizeof lbuf,
1.11      d         211:                    "        (%s) at %d:%02d ...", ttynam,
1.1       deraadt   212:                    lt->tm_hour, lt->tm_min);
                    213:                (void)fprintf(fp, "%-79.79s\r\n", lbuf);
                    214:        }
                    215:        (void)fprintf(fp, "%79s\r\n", " ");
                    216:
1.13      deraadt   217:        if (fname) {
                    218:                gid_t egid = getegid();
                    219:
                    220:                setegid(getgid());
                    221:                if (freopen(fname, "r", stdin) == NULL)
1.21      henning   222:                        err(1, "can't read %s", fname);
1.13      deraadt   223:                setegid(egid);
                    224:        }
1.1       deraadt   225:        while (fgets(lbuf, sizeof(lbuf), stdin))
                    226:                for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
1.5       deraadt   227:                        vis(tmpbuf, ch, VIS_SAFE|VIS_NOSLASH, p[1]);
1.4       deraadt   228:                        if (cnt == 79+1-strlen(tmpbuf) || ch == '\n') {
                    229:                                for (; cnt < 79+1-strlen(tmpbuf); ++cnt)
1.1       deraadt   230:                                        putc(' ', fp);
                    231:                                putc('\r', fp);
                    232:                                putc('\n', fp);
                    233:                                cnt = -1;
                    234:                        }
1.4       deraadt   235:                        if (ch != '\n') {
                    236:                                int xx;
                    237:
                    238:                                for (xx = 0; tmpbuf[xx]; xx++)
                    239:                                        putc(tmpbuf[xx], fp);
                    240:                        }
1.1       deraadt   241:                }
                    242:        (void)fprintf(fp, "%79s\r\n", " ");
                    243:        rewind(fp);
                    244:
1.9       form      245:        if (fstat(fd, &sbuf))
1.21      henning   246:                err(1, "can't stat temporary file");
1.1       deraadt   247:        mbufsize = sbuf.st_size;
1.22      tom       248:        mbuf = malloc((u_int)mbufsize);
                    249:        if (mbuf == NULL)
                    250:                err(1, NULL);
1.9       form      251:        if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
1.21      henning   252:                err(1, "can't read temporary file");
1.1       deraadt   253:        (void)close(fd);
1.15      millert   254: }
                    255:
                    256: void
                    257: addgroup(struct group *grp, char *name)
                    258: {
                    259:        int i;
                    260:        struct wallgroup *g;
                    261:
                    262:        for (i = 0; grp->gr_mem[i]; i++)
                    263:                ;
                    264:
1.27      deraadt   265:        g = malloc(sizeof *g);
1.15      millert   266:        if (g == NULL)
1.22      tom       267:                err(1, NULL);
1.15      millert   268:        g->gid = grp->gr_gid;
                    269:        g->name = name;
1.27      deraadt   270:        g->mem = calloc(i + 1, sizeof(char *));
1.15      millert   271:        if (g->mem == NULL)
1.22      tom       272:                err(1, NULL);
1.15      millert   273:        for (i = 0; grp->gr_mem[i] != NULL; i++) {
                    274:                g->mem[i] = strdup(grp->gr_mem[i]);
                    275:                if (g->mem[i] == NULL)
1.22      tom       276:                        err(1, NULL);
1.15      millert   277:        }
                    278:        g->mem[i] = NULL;
                    279:        g->next = grouplist;
                    280:        grouplist = g;
                    281: }
                    282:
                    283: void
                    284: usage(void)
                    285: {
                    286:        extern char *__progname;
                    287:
                    288:        (void)fprintf(stderr, "usage: %s [-g group] [file]\n", __progname);
                    289:        exit(1);
1.1       deraadt   290: }