1 /* $OpenBSD: tty_tty.c,v 1.32 2022/08/14 01:58:28 jsg Exp $ */ 2 /* $NetBSD: tty_tty.c,v 1.13 1996/03/30 22:24:46 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 1982, 1986, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)tty_tty.c 8.2 (Berkeley) 9/23/93 33 */ 34 35 /* 36 * Indirect driver for controlling tty. 37 */ 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/proc.h> 41 #include <sys/tty.h> 42 #include <sys/vnode.h> 43 #include <sys/lock.h> 44 #include <sys/fcntl.h> 45 46 47 #define cttyvp(p) \ 48 ((p)->p_p->ps_flags & PS_CONTROLT ? \ 49 (p)->p_p->ps_session->s_ttyvp : NULL) 50 51 int 52 cttyopen(dev_t dev, int flag, int mode, struct proc *p) 53 { 54 struct vnode *ttyvp = cttyvp(p); 55 int error; 56 57 if (ttyvp == NULL) 58 return (ENXIO); 59 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY); 60 error = VOP_OPEN(ttyvp, flag, NOCRED, p); 61 VOP_UNLOCK(ttyvp); 62 return (error); 63 } 64 65 int 66 cttyread(dev_t dev, struct uio *uio, int flag) 67 { 68 struct vnode *ttyvp = cttyvp(uio->uio_procp); 69 int error; 70 71 if (ttyvp == NULL) 72 return (EIO); 73 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY); 74 error = VOP_READ(ttyvp, uio, flag, NOCRED); 75 VOP_UNLOCK(ttyvp); 76 return (error); 77 } 78 79 int 80 cttywrite(dev_t dev, struct uio *uio, int flag) 81 { 82 struct vnode *ttyvp = cttyvp(uio->uio_procp); 83 int error; 84 85 if (ttyvp == NULL) 86 return (EIO); 87 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY); 88 error = VOP_WRITE(ttyvp, uio, flag, NOCRED); 89 VOP_UNLOCK(ttyvp); 90 return (error); 91 } 92 93 int 94 cttyioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 95 { 96 struct vnode *ttyvp = cttyvp(p); 97 struct session *sess; 98 int error, secs; 99 100 if (ttyvp == NULL) 101 return (EIO); 102 if (cmd == TIOCSCTTY) /* XXX */ 103 return (EINVAL); 104 if (cmd == TIOCNOTTY) { 105 if (!SESS_LEADER(p->p_p)) { 106 atomic_clearbits_int(&p->p_p->ps_flags, PS_CONTROLT); 107 return (0); 108 } else 109 return (EINVAL); 110 } 111 switch (cmd) { 112 case TIOCSETVERAUTH: 113 if ((error = suser(p))) 114 return error; 115 secs = *(int *)addr; 116 if (secs < 1 || secs > 3600) 117 return EINVAL; 118 sess = p->p_p->ps_pgrp->pg_session; 119 sess->s_verauthuid = p->p_ucred->cr_ruid; 120 sess->s_verauthppid = p->p_p->ps_pptr->ps_pid; 121 timeout_add_sec(&sess->s_verauthto, secs); 122 return 0; 123 case TIOCCLRVERAUTH: 124 sess = p->p_p->ps_pgrp->pg_session; 125 timeout_del(&sess->s_verauthto); 126 zapverauth(sess); 127 return 0; 128 case TIOCCHKVERAUTH: 129 /* 130 * It's not clear when or what these checks are for. 131 * How can we reach this code with a different ruid? 132 * The ppid check is also more porous than desired. 133 * Nevertheless, the checks reflect the original intention; 134 * namely, that it be the same user using the same shell. 135 */ 136 sess = p->p_p->ps_pgrp->pg_session; 137 if (sess->s_verauthuid == p->p_ucred->cr_ruid && 138 sess->s_verauthppid == p->p_p->ps_pptr->ps_pid) 139 return 0; 140 return EPERM; 141 } 142 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p)); 143 } 144 145 int 146 cttykqfilter(dev_t dev, struct knote *kn) 147 { 148 struct vnode *ttyvp = cttyvp(curproc); 149 150 if (ttyvp == NULL) { 151 if (kn->kn_flags & (__EV_POLL | __EV_SELECT)) 152 return (seltrue_kqfilter(dev, kn)); 153 return (ENXIO); 154 } 155 return (VOP_KQFILTER(ttyvp, FREAD|FWRITE, kn)); 156 } 157