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