Exemple #1
0
/*
 * do_write - actually make the connection
 */
void
do_write(char *tty, char *mytty, uid_t myuid)
{
	char *login, *nows;
	struct passwd *pwd;
	time_t now;
	char path[PATH_MAX], host[HOST_NAME_MAX+1], line[512];
	gid_t gid;
	int fd;

	/* Determine our login name before the we reopen() stdout */
	if ((login = getlogin()) == NULL) {
		if ((pwd = getpwuid(myuid)))
			login = pwd->pw_name;
		else
			login = "******";
	}

	(void)snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
	fd = open(path, O_WRONLY, 0666);
	if (fd == -1)
		err(1, "open %s", path);
	fflush(stdout);
	if (dup2(fd, STDOUT_FILENO) == -1)
		err(1, "dup2 %s", path);
	if (fd != STDOUT_FILENO)
		close(fd);

	/* revoke privs, now that we have opened the tty */
	gid = getgid();
	if (setresgid(gid, gid, gid) == -1)
		err(1, "setresgid");

	/*
	 * Unfortunately this is rather late - well after utmp
	 * parsing, then pinned by the tty open and setresgid
	 */
	if (pledge("stdio", NULL) == -1)
		err(1, "pledge");

	(void)signal(SIGINT, done);
	(void)signal(SIGHUP, done);

	/* print greeting */
	if (gethostname(host, sizeof(host)) < 0)
		(void)strlcpy(host, "???", sizeof host);
	now = time(NULL);
	nows = ctime(&now);
	nows[16] = '\0';
	(void)printf("\r\n\007\007\007Message from %s@%s on %s at %s ...\r\n",
	    login, host, mytty, nows + 11);

	while (fgets(line, sizeof(line), stdin) != NULL)
		wr_fputs(line);
}
Exemple #2
0
/*
 * do_write - actually make the connection
 */
static void
do_write(const char *tty, const char *mytty, uid_t myuid, int *mymsgok)
{
	const char *login;
	char *nows;
	struct passwd *pwd;
	time_t now;
	char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];

	/* Determine our login name before we reopen() stdout */
	if ((login = getlogin()) == NULL) {
		if ((pwd = getpwuid(myuid)))
			login = pwd->pw_name;
		else
			login = "******";
	}

	snprintf(path, sizeof(path), "%s%s", _PATH_DEV, tty);
	if ((freopen(path, "w", stdout)) == NULL)
		err(1, "%s", path);

	signal(SIGINT, done);
	signal(SIGHUP, done);

	/* print greeting */
	if (gethostname(host, sizeof(host)) < 0)
		strlcpy(host, "???", sizeof host);
	now = time(NULL);
	nows = ctime(&now);
	nows[16] = '\0';
	printf("\r\n\007\007\007Message from %s@%s on %s at %s (%s %s replies)...\r\n",
	    login, host, mytty, nows + 11, login, *mymsgok == 0 ? "does not accept" : "accepts");

	while (fgets(line, sizeof(line), stdin) != NULL)
		wr_fputs(line);
}