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