Home | History | Annotate | Line # | Download | only in write
term_chk.c revision 1.8
      1  1.8     lukem /* $NetBSD: term_chk.c,v 1.8 2009/04/14 07:59:17 lukem Exp $ */
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1989, 1993
      5  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to Berkeley by
      8  1.1  christos  * Jef Poskanzer and Craig Leres of the Lawrence Berkeley Laboratory.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.4       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.1  christos  *    may be used to endorse or promote products derived from this software
     20  1.1  christos  *    without specific prior written permission.
     21  1.1  christos  *
     22  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  christos  * SUCH DAMAGE.
     33  1.1  christos  */
     34  1.1  christos 
     35  1.1  christos #include <sys/cdefs.h>
     36  1.1  christos #ifndef lint
     37  1.8     lukem __RCSID("$NetBSD: term_chk.c,v 1.8 2009/04/14 07:59:17 lukem Exp $");
     38  1.1  christos #endif
     39  1.1  christos 
     40  1.1  christos #include <sys/types.h>
     41  1.1  christos #include <sys/param.h>
     42  1.1  christos #include <sys/stat.h>
     43  1.1  christos #include <time.h>
     44  1.1  christos #include <stdio.h>
     45  1.1  christos #include <errno.h>
     46  1.1  christos #include <unistd.h>
     47  1.1  christos #include <paths.h>
     48  1.1  christos #include <fcntl.h>
     49  1.1  christos #include <string.h>
     50  1.1  christos #include <err.h>
     51  1.1  christos 
     52  1.1  christos #include "term_chk.h"
     53  1.1  christos 
     54  1.1  christos /*
     55  1.1  christos  * term_chk - check that a terminal exists, and get the message bit
     56  1.1  christos  *     and the access time
     57  1.1  christos  */
     58  1.1  christos int
     59  1.1  christos term_chk(uid_t uid, const char *tty, int *msgsokP, time_t *atimeP, int ismytty,
     60  1.1  christos     gid_t saved_egid)
     61  1.1  christos {
     62  1.1  christos 	char path[MAXPATHLEN];
     63  1.1  christos 	struct stat s;
     64  1.1  christos 	int i, fd, serrno;
     65  1.1  christos 
     66  1.7  christos 	if (strstr(tty, "../") != NULL) {
     67  1.2  christos 		errno = EINVAL;
     68  1.2  christos 		return -1;
     69  1.1  christos 	}
     70  1.1  christos 	i = snprintf(path, sizeof path, _PATH_DEV "%s", tty);
     71  1.8     lukem 	if (i < 0 || i >= (int)sizeof(path)) {
     72  1.2  christos 		errno = ENOMEM;
     73  1.2  christos 		return -1;
     74  1.1  christos 	}
     75  1.1  christos 
     76  1.1  christos 	(void)setegid(saved_egid);
     77  1.1  christos 	fd = open(path, O_WRONLY, 0);
     78  1.1  christos 	serrno = errno;
     79  1.1  christos 	(void)setegid(getgid());
     80  1.1  christos 	errno = serrno;
     81  1.1  christos 
     82  1.1  christos 	if (fd == -1)
     83  1.1  christos 		return(-1);
     84  1.1  christos 	if (fstat(fd, &s) == -1)
     85  1.1  christos 		goto error;
     86  1.2  christos 	if (!isatty(fd))
     87  1.1  christos 		goto error;
     88  1.3  christos 	if (s.st_uid != uid && uid != 0) {
     89  1.2  christos 		errno = EPERM;
     90  1.2  christos 		goto error;
     91  1.2  christos 	}
     92  1.6  christos 	if (msgsokP)
     93  1.6  christos 		*msgsokP = (s.st_mode & S_IWGRP) != 0;	/* group write bit */
     94  1.6  christos 	if (atimeP)
     95  1.6  christos 		*atimeP = s.st_atime;
     96  1.1  christos 	if (ismytty)
     97  1.2  christos 		(void)close(fd);
     98  1.2  christos 	return ismytty ? 0 : fd;
     99  1.1  christos error:
    100  1.1  christos 	if (fd != -1) {
    101  1.1  christos 		serrno = errno;
    102  1.2  christos 		(void)close(fd);
    103  1.1  christos 		errno = serrno;
    104  1.1  christos 	}
    105  1.2  christos 	return -1;
    106  1.1  christos }
    107  1.1  christos 
    108  1.1  christos char *
    109  1.1  christos check_sender(time_t *atime, uid_t myuid, gid_t saved_egid)
    110  1.1  christos {
    111  1.1  christos 	int myttyfd;
    112  1.1  christos 	int msgsok;
    113  1.1  christos 	char *mytty;
    114  1.1  christos 
    115  1.1  christos 	/* check that sender has write enabled */
    116  1.1  christos 	if (isatty(fileno(stdin)))
    117  1.1  christos 		myttyfd = fileno(stdin);
    118  1.1  christos 	else if (isatty(fileno(stdout)))
    119  1.1  christos 		myttyfd = fileno(stdout);
    120  1.1  christos 	else if (isatty(fileno(stderr)))
    121  1.1  christos 		myttyfd = fileno(stderr);
    122  1.6  christos 	else if (atime == NULL)
    123  1.5  christos 		return NULL;
    124  1.1  christos 	else
    125  1.5  christos 		errx(1, "Cannot find your tty");
    126  1.5  christos 	if ((mytty = ttyname(myttyfd)) == NULL)
    127  1.5  christos 		err(1, "Cannot find the name of your tty");
    128  1.7  christos 	if (strncmp(mytty, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
    129  1.7  christos 		mytty += sizeof(_PATH_DEV) - 1;
    130  1.1  christos 	if (term_chk(myuid, mytty, &msgsok, atime, 1, saved_egid) == -1)
    131  1.1  christos 		err(1, "%s%s", _PATH_DEV, mytty);
    132  1.1  christos 	if (!msgsok) {
    133  1.1  christos 		warnx(
    134  1.1  christos 		    "You have write permission turned off; no reply possible");
    135  1.1  christos 	}
    136  1.1  christos 	return mytty;
    137  1.1  christos }
    138