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