Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / wall / wall.c
1 /*
2  * Copyright (c) 1988, 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1988, 1990, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)wall.c   8.2 (Berkeley) 11/16/93
35  * $FreeBSD: src/usr.bin/wall/wall.c,v 1.13.2.6 2001/10/18 08:08:17 des Exp $
36  * $DragonFly: src/usr.bin/wall/wall.c,v 1.2 2003/06/17 04:29:33 dillon Exp $
37  */
38
39 /*
40  * This program is not related to David Wall, whose Stanford Ph.D. thesis
41  * is entitled "Mechanisms for Broadcast and Selective Broadcast".
42  */
43
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <sys/uio.h>
47
48 #include <ctype.h>
49 #include <err.h>
50 #include <grp.h>
51 #include <locale.h>
52 #include <paths.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #include <unistd.h>
59 #include <utmp.h>
60
61 #include "ttymsg.h"
62
63 static void makemsg(char *);
64 static void usage(void);
65
66 #define IGNOREUSER      "sleeper"
67
68 struct wallgroup {
69         struct wallgroup *next;
70         char            *name;
71         gid_t           gid;
72 } *grouplist;
73 int nobanner;
74 int mbufsize;
75 char *mbuf;
76
77 int
78 main(int argc, char *argv[])
79 {
80         struct iovec iov;
81         struct utmp utmp;
82         int ch;
83         int ingroup;
84         FILE *fp;
85         struct wallgroup *g;
86         struct group *grp;
87         char **np;
88         const char *p;
89         struct passwd *pw;
90         char line[sizeof(utmp.ut_line) + 1];
91         char username[sizeof(utmp.ut_name) + 1];
92
93         (void)setlocale(LC_CTYPE, "");
94
95         while ((ch = getopt(argc, argv, "g:n")) != -1)
96                 switch (ch) {
97                 case 'n':
98                         /* undoc option for shutdown: suppress banner */
99                         if (geteuid() == 0)
100                                 nobanner = 1;
101                         break;
102                 case 'g':
103                         g = (struct wallgroup *)malloc(sizeof *g);
104                         g->next = grouplist;
105                         g->name = optarg;
106                         g->gid = -1;
107                         grouplist = g;
108                         break;
109                 case '?':
110                 default:
111                         usage();
112                 }
113         argc -= optind;
114         argv += optind;
115         if (argc > 1)
116                 usage();
117
118         for (g = grouplist; g; g = g->next) {
119                 grp = getgrnam(g->name);
120                 if (grp != NULL)
121                         g->gid = grp->gr_gid;
122                 else
123                         warnx("%s: no such group", g->name);
124         }
125
126         makemsg(*argv);
127
128         if (!(fp = fopen(_PATH_UTMP, "r")))
129                 err(1, "cannot read %s", _PATH_UTMP);
130         iov.iov_base = mbuf;
131         iov.iov_len = mbufsize;
132         /* NOSTRICT */
133         while (fread((char *)&utmp, sizeof(utmp), 1, fp) == 1) {
134                 if (!utmp.ut_name[0] ||
135                     !strncmp(utmp.ut_name, IGNOREUSER, sizeof(utmp.ut_name)))
136                         continue;
137                 if (grouplist) {
138                         ingroup = 0;
139                         strlcpy(username, utmp.ut_name, sizeof(utmp.ut_name));
140                         pw = getpwnam(username);
141                         if (!pw)
142                                 continue;
143                         for (g = grouplist; g && ingroup == 0; g = g->next) {
144                                 if (g->gid == -1)
145                                         continue;
146                                 if (g->gid == pw->pw_gid)
147                                         ingroup = 1;
148                                 else if ((grp = getgrgid(g->gid)) != NULL) {
149                                         for (np = grp->gr_mem; *np; np++) {
150                                                 if (strcmp(*np, username) == 0) {
151                                                         ingroup = 1;
152                                                         break;
153                                                 }
154                                         }
155                                 }
156                         }
157                         if (ingroup == 0)
158                                 continue;
159                 }
160                 strncpy(line, utmp.ut_line, sizeof(utmp.ut_line));
161                 line[sizeof(utmp.ut_line)] = '\0';
162                 if ((p = ttymsg(&iov, 1, line, 60*5)) != NULL)
163                         warnx("%s", p);
164         }
165         exit(0);
166 }
167
168 static void
169 usage()
170 {
171         (void)fprintf(stderr, "usage: wall [-g group] [file]\n");
172         exit(1);
173 }
174
175 void
176 makemsg(char *fname)
177 {
178         int cnt;
179         unsigned char ch;
180         struct tm *lt;
181         struct passwd *pw;
182         struct stat sbuf;
183         time_t now;
184         FILE *fp;
185         int fd;
186         char *p, *tty, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[64];
187         const char *whom;
188         gid_t egid;
189
190         (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
191         if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
192                 err(1, "can't open temporary file");
193         (void)unlink(tmpname);
194
195         if (!nobanner) {
196                 tty = ttyname(STDERR_FILENO);
197                 if (tty == NULL)
198                         tty = "no tty";
199
200                 if (!(whom = getlogin()))
201                         whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
202                 (void)gethostname(hostname, sizeof(hostname));
203                 (void)time(&now);
204                 lt = localtime(&now);
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", " ");
214                 (void)snprintf(lbuf, sizeof(lbuf), 
215                     "Broadcast Message from %s@%s",
216                     whom, hostname);
217                 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
218                 (void)snprintf(lbuf, sizeof(lbuf),
219                     "        (%s) at %d:%02d %s...", tty,
220                     lt->tm_hour, lt->tm_min, lt->tm_zone);
221                 (void)fprintf(fp, "%-79.79s\r\n", lbuf);
222         }
223         (void)fprintf(fp, "%79s\r\n", " ");
224
225         if (fname) {
226                 egid = getegid();
227                 setegid(getgid());
228                 if (freopen(fname, "r", stdin) == NULL)
229                         err(1, "can't read %s", fname);
230                 setegid(egid);
231         }
232         while (fgets(lbuf, sizeof(lbuf), stdin))
233                 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
234                         if (ch == '\r') {
235                                 cnt = 0;
236                         } else if (cnt == 79 || ch == '\n') {
237                                 for (; cnt < 79; ++cnt)
238                                         putc(' ', fp);
239                                 putc('\r', fp);
240                                 putc('\n', fp);
241                                 cnt = 0;
242                         } else if (((ch & 0x80) && ch < 0xA0) ||
243                                    /* disable upper controls */
244                                    (!isprint(ch) && !isspace(ch) &&
245                                     ch != '\a' && ch != '\b')
246                                   ) {
247                                 if (ch & 0x80) {
248                                         ch &= 0x7F;
249                                         putc('M', fp);
250                                         if (++cnt == 79) {
251                                                 putc('\r', fp);
252                                                 putc('\n', fp);
253                                                 cnt = 0;
254                                         }
255                                         putc('-', fp);
256                                         if (++cnt == 79) {
257                                                 putc('\r', fp);
258                                                 putc('\n', fp);
259                                                 cnt = 0;
260                                         }
261                                 }
262                                 if (iscntrl(ch)) {
263                                         ch ^= 040;
264                                         putc('^', fp);
265                                         if (++cnt == 79) {
266                                                 putc('\r', fp);
267                                                 putc('\n', fp);
268                                                 cnt = 0;
269                                         }
270                                 }
271                                 putc(ch, fp);
272                         } else {
273                                 putc(ch, fp);
274                         }
275                 }
276         (void)fprintf(fp, "%79s\r\n", " ");
277         rewind(fp);
278
279         if (fstat(fd, &sbuf))
280                 err(1, "can't stat temporary file");
281         mbufsize = sbuf.st_size;
282         if (!(mbuf = malloc((u_int)mbufsize)))
283                 err(1, "out of memory");
284         if (fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
285                 err(1, "can't read temporary file");
286         (void)close(fd);
287 }