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