NetBSD/usr.bin/write/term_chk.c

138 lines
3.9 KiB
C
Raw Normal View History

2009-04-14 11:59:17 +04:00
/* $NetBSD: term_chk.c,v 1.8 2009/04/14 07:59:17 lukem Exp $ */
2003-04-21 03:53:04 +04:00
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
2003-04-21 03:53:04 +04:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
2009-04-14 11:59:17 +04:00
__RCSID("$NetBSD: term_chk.c,v 1.8 2009/04/14 07:59:17 lukem Exp $");
2003-04-21 03:53:04 +04:00
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <paths.h>
#include <fcntl.h>
#include <string.h>
#include <err.h>
#include "term_chk.h"
/*
* term_chk - check that a terminal exists, and get the message bit
* and the access time
*/
int
term_chk(uid_t uid, const char *tty, int *msgsokP, time_t *atimeP, int ismytty,
gid_t saved_egid)
{
char path[MAXPATHLEN];
struct stat s;
int i, fd, serrno;
if (strstr(tty, "../") != NULL) {
2003-05-03 19:57:11 +04:00
errno = EINVAL;
return -1;
2003-04-21 03:53:04 +04:00
}
i = snprintf(path, sizeof path, _PATH_DEV "%s", tty);
2009-04-14 11:59:17 +04:00
if (i < 0 || i >= (int)sizeof(path)) {
2003-05-03 19:57:11 +04:00
errno = ENOMEM;
return -1;
2003-04-21 03:53:04 +04:00
}
(void)setegid(saved_egid);
fd = open(path, O_WRONLY, 0);
serrno = errno;
(void)setegid(getgid());
errno = serrno;
if (fd == -1)
return(-1);
if (fstat(fd, &s) == -1)
goto error;
2003-05-03 19:57:11 +04:00
if (!isatty(fd))
2003-04-21 03:53:04 +04:00
goto error;
if (s.st_uid != uid && uid != 0) {
2003-05-03 19:57:11 +04:00
errno = EPERM;
goto error;
}
if (msgsokP)
*msgsokP = (s.st_mode & S_IWGRP) != 0; /* group write bit */
if (atimeP)
*atimeP = s.st_atime;
2003-04-21 03:53:04 +04:00
if (ismytty)
2003-05-03 19:57:11 +04:00
(void)close(fd);
return ismytty ? 0 : fd;
2003-04-21 03:53:04 +04:00
error:
if (fd != -1) {
serrno = errno;
2003-05-03 19:57:11 +04:00
(void)close(fd);
2003-04-21 03:53:04 +04:00
errno = serrno;
}
2003-05-03 19:57:11 +04:00
return -1;
2003-04-21 03:53:04 +04:00
}
char *
check_sender(time_t *atime, uid_t myuid, gid_t saved_egid)
{
int myttyfd;
int msgsok;
char *mytty;
/* check that sender has write enabled */
if (isatty(fileno(stdin)))
myttyfd = fileno(stdin);
else if (isatty(fileno(stdout)))
myttyfd = fileno(stdout);
else if (isatty(fileno(stderr)))
myttyfd = fileno(stderr);
else if (atime == NULL)
return NULL;
2003-04-21 03:53:04 +04:00
else
errx(1, "Cannot find your tty");
if ((mytty = ttyname(myttyfd)) == NULL)
err(1, "Cannot find the name of your tty");
if (strncmp(mytty, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
mytty += sizeof(_PATH_DEV) - 1;
2003-04-21 03:53:04 +04:00
if (term_chk(myuid, mytty, &msgsok, atime, 1, saved_egid) == -1)
err(1, "%s%s", _PATH_DEV, mytty);
if (!msgsok) {
warnx(
"You have write permission turned off; no reply possible");
}
return mytty;
}