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