72dfae8dc4fe139d144c175dc51f5f56fc4dd5c2
[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.4 2005/08/31 16:27:53 liamfoy 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 "utmpentry.h"
60
61 #include "ttymsg.h"
62
63 static void makemsg(char *);
64 static void usage(void);
65
66 struct wallgroup {
67         struct wallgroup *next;
68         char            *name;
69         gid_t           gid;
70 } *grouplist;
71 int nobanner;
72 int mbufsize;
73 char *mbuf;
74
75 int
76 main(int argc, char *argv[])
77 {
78         struct iovec iov;
79         struct utmpentry *ep;
80         int ch;
81         int ingroup;
82         FILE *fp;
83         struct wallgroup *g;
84         struct group *grp;
85         char **np;
86         const char *p;
87         struct passwd *pw;
88
89         (void)setlocale(LC_CTYPE, "");
90
91         while ((ch = getopt(argc, argv, "g:n")) != -1)
92                 switch (ch) {
93                 case 'n':
94                         /* undoc option for shutdown: suppress banner */
95                         if (geteuid() == 0)
96                                 nobanner = 1;
97                         break;
98                 case 'g':
99                         g = (struct wallgroup *)malloc(sizeof *g);
100                         g->next = grouplist;
101                         g->name = optarg;
102                         g->gid = -1;
103                         grouplist = g;
104                         break;
105                 case '?':
106                 default:
107                         usage();
108                 }
109         argc -= optind;
110         argv += optind;
111         if (argc > 1)
112                 usage();
113
114         for (g = grouplist; g; g = g->next) {
115                 grp = getgrnam(g->name);
116                 if (grp != NULL)
117                         g->gid = grp->gr_gid;
118                 else
119                         warnx("%s: no such group", g->name);
120         }
121
122         makemsg(*argv);
123
124         iov.iov_base = mbuf;
125         iov.iov_len = mbufsize;
126
127         getutentries(NULL, &ep);
128         /* NOSTRICT */
129         for (; ep; ep = ep->next) {
130                 if (grouplist) {
131                         ingroup = 0;
132                         pw = getpwnam(ep->name);
133                         if (!pw)
134                                 continue;
135                         for (g = grouplist; g && ingroup == 0; g = g->next) {
136                                 if (g->gid == (gid_t)-1)
137                                         continue;
138                                 if (g->gid == pw->pw_gid)
139                                         ingroup = 1;
140                                 else if ((grp = getgrgid(g->gid)) != NULL) {
141                                         for (np = grp->gr_mem; *np; np++) {
142                                                 if (strcmp(*np, username) == 0) {
143                                                         ingroup = 1;
144                                                         break;
145                                                 }
146                                         }
147                                 }
148                         }
149                         if (ingroup == 0)
150                                 continue;
151                 }
152                 /* skip [xgk]dm/xserver entries (":0", ":1", etc.) */
153                 if (ep->line[0] == ':' && isdigit((unsigned char)ep->line[1]))
154                         continue;
155                 
156                 if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL)
157                         warnx("%s", p);
158         }
159         exit(0);
160 }
161
162 static void
163 usage(void)
164 {
165         (void)fprintf(stderr, "usage: wall [-g group] [file]\n");
166         exit(1);
167 }
168
169 void
170 makemsg(char *fname)
171 {
172         int cnt;
173         unsigned char ch;
174         struct tm *lt;
175         struct passwd *pw;
176         struct stat sbuf;
177         time_t now;
178         FILE *fp;
179         int fd;
180         char *p, hostname[MAXHOSTNAMELEN], lbuf[256], tmpname[MAXPATHLEN];
181         const char *tty;
182         const char *whom;
183         gid_t egid;
184
185         (void)snprintf(tmpname, sizeof(tmpname), "%s/wall.XXXXXX", _PATH_TMP);
186         if ((fd = mkstemp(tmpname)) == -1 || !(fp = fdopen(fd, "r+")))
187                 err(1, "can't open temporary file");
188         (void)unlink(tmpname);
189
190         if (!nobanner) {
191                 tty = ttyname(STDERR_FILENO);
192                 if (tty == NULL)
193                         tty = "no tty";
194
195                 if (!(whom = getlogin()))
196                         whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
197                 (void)gethostname(hostname, sizeof(hostname));
198                 (void)time(&now);
199                 lt = localtime(&now);
200
201                 /*
202                  * all this stuff is to blank out a square for the message;
203                  * we wrap message lines at column 79, not 80, because some
204                  * terminals wrap after 79, some do not, and we can't tell.
205                  * Which means that we may leave a non-blank character
206                  * in column 80, but that can't be helped.
207                  */
208                 (void)fprintf(fp, "\r%79s\r\n", " ");
209                 (void)snprintf(lbuf, sizeof(lbuf), 
210                     "Broadcast Message from %s@%s",
211                     whom, hostname);
212                 (void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
213                 (void)snprintf(lbuf, sizeof(lbuf),
214                     "        (%s) at %d:%02d %s...", tty,
215                     lt->tm_hour, lt->tm_min, lt->tm_zone);
216                 (void)fprintf(fp, "%-79.79s\r\n", lbuf);
217         }
218         (void)fprintf(fp, "%79s\r\n", " ");
219
220         if (fname) {
221                 egid = getegid();
222                 setegid(getgid());
223                 if (freopen(fname, "r", stdin) == NULL)
224                         err(1, "can't read %s", fname);
225                 setegid(egid);
226         }
227         while (fgets(lbuf, sizeof(lbuf), stdin))
228                 for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
229                         if (ch == '\r') {
230                                 cnt = 0;
231                         } else if (cnt == 79 || ch == '\n') {
232                                 for (; cnt < 79; ++cnt)
233                                         putc(' ', fp);
234                                 putc('\r', fp);
235                                 putc('\n', fp);
236                                 cnt = 0;
237                         } else if (((ch & 0x80) && ch < 0xA0) ||
238                                    /* disable upper controls */
239                                    (!isprint(ch) && !isspace(ch) &&
240                                     ch != '\a' && ch != '\b')
241                                   ) {
242                                 if (ch & 0x80) {
243                                         ch &= 0x7F;
244                                         putc('M', fp);
245                                         if (++cnt == 79) {
246                                                 putc('\r', fp);
247                                                 putc('\n', fp);
248                                                 cnt = 0;
249                                         }
250                                         putc('-', fp);
251                                         if (++cnt == 79) {
252                                                 putc('\r', fp);
253                                                 putc('\n', fp);
254                                                 cnt = 0;
255                                         }
256                                 }
257                                 if (iscntrl(ch)) {
258                                         ch ^= 040;
259                                         putc('^', fp);
260                                         if (++cnt == 79) {
261                                                 putc('\r', fp);
262                                                 putc('\n', fp);
263                                                 cnt = 0;
264                                         }
265                                 }
266                                 putc(ch, fp);
267                         } else {
268                                 putc(ch, fp);
269                         }
270                 }
271         (void)fprintf(fp, "%79s\r\n", " ");
272         rewind(fp);
273
274         if (fstat(fd, &sbuf))
275                 err(1, "can't stat temporary file");
276         mbufsize = sbuf.st_size;
277         if (!(mbuf = malloc((u_int)mbufsize)))
278                 err(1, "out of memory");
279         if ((int)fread(mbuf, sizeof(*mbuf), mbufsize, fp) != mbufsize)
280                 err(1, "can't read temporary file");
281         (void)close(fd);
282 }