1 /* $NetBSD: term_chk.c,v 1.3 2003/05/13 23:05:34 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.3 2003/05/13 23:05:34 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; 72 return -1; 73 } 74 i = snprintf(path, sizeof path, _PATH_DEV "%s", tty); 75 if (i < 0 || i >= sizeof(path)) { 76 errno = ENOMEM; 77 return -1; 78 } 79 80 (void)setegid(saved_egid); 81 fd = open(path, O_WRONLY, 0); 82 serrno = errno; 83 (void)setegid(getgid()); 84 errno = serrno; 85 86 if (fd == -1) 87 return(-1); 88 if (fstat(fd, &s) == -1) 89 goto error; 90 if (!isatty(fd)) 91 goto error; 92 if (s.st_uid != uid && uid != 0) { 93 errno = EPERM; 94 goto error; 95 } 96 *msgsokP = (s.st_mode & S_IWGRP) != 0; /* group write bit */ 97 *atimeP = s.st_atime; 98 if (ismytty) 99 (void)close(fd); 100 return ismytty ? 0 : fd; 101 error: 102 if (fd != -1) { 103 serrno = errno; 104 (void)close(fd); 105 errno = serrno; 106 } 107 return -1; 108 } 109 110 char * 111 check_sender(time_t *atime, uid_t myuid, gid_t saved_egid) 112 { 113 int myttyfd; 114 int msgsok; 115 char *mytty; 116 char *cp; 117 118 /* check that sender has write enabled */ 119 if (isatty(fileno(stdin))) 120 myttyfd = fileno(stdin); 121 else if (isatty(fileno(stdout))) 122 myttyfd = fileno(stdout); 123 else if (isatty(fileno(stderr))) 124 myttyfd = fileno(stderr); 125 else 126 errx(1, "can't find your tty"); 127 if (!(mytty = ttyname(myttyfd))) 128 errx(1, "can't find your tty's name"); 129 if ((cp = strrchr(mytty, '/')) != NULL) 130 mytty = cp + 1; 131 if (term_chk(myuid, mytty, &msgsok, atime, 1, saved_egid) == -1) 132 err(1, "%s%s", _PATH_DEV, mytty); 133 if (!msgsok) { 134 warnx( 135 "You have write permission turned off; no reply possible"); 136 } 137 return mytty; 138 } 139