]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - libexec/pt_chown/pt_chown.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / libexec / pt_chown / pt_chown.c
1 /*
2  * Copyright (c) 2002 The FreeBSD Project, Inc.
3  * All rights reserved.
4  *
5  * This software includes code contributed to the FreeBSD Project
6  * by Ryan Younce of North Carolina State University.
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 FreeBSD Project nor the names of its
17  *    contributors may be used to endorse or promote products derived from
18  *    this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT AND CONTRIBUTORS ``AS IS''
21  * AND 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 FREEBSD PROJECT OR ITS CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 #ifndef lint
35 __FBSDID("$FreeBSD$");
36 #endif /* not lint */
37
38 #include <sys/stat.h>
39
40 #include <grp.h>
41 #include <stdlib.h>
42 #include <sysexits.h>
43 #include <unistd.h>
44
45 /*
46  * pt_chown
47  * Utility support routine for grantpt(3).
48  *
49  * According to IEEE Std 1003.1-2001, grantpt(3) changes ownership and
50  * permission bits of a slave pseudo-terminal device associated with a
51  * master.
52  *
53  * Since doing this if we are not the owner of the slave (which would
54  * rarely happen) cannot be done by conventional methods, grantpt(3)
55  * has to rely on this support program, which is setuid root, to change
56  * the slave's owner, group, and permission mode attributes.  It's
57  * a rather undesirable approach, but Digital Unix and Solaris also seem
58  * to rely on this approach to pull this off.
59  *
60  * This program hangs around long enough to do just these things upon
61  * its standard input (which is set up by grantpt(3) to be the master's
62  * descriptor, the one passed to it).  The rationale behind this
63  * approach not allowing somebody to modify ownership of another active
64  * pseudo terminal is:
65  *
66  * 1)  This program only operates on its standard input.  If STDIN_FILENO
67  *     is not open or is not a pseudo-terminal master, no action is
68  *     taken and the program exits (ptsname() is called for a non-NULL
69  *     return).
70  * 2)  Only one active file description for a pseudo-terminal master
71  *     can exist at a time (attempting to open an active PTY returns with
72  *     EIO - I/O Error).  Thus, if the pseudo-terminal is already in
73  *     use by somebody else, it could not have been opened to begin
74  *     with, and thus this program would be useless in such situations.
75  */
76 int
77 main(int argc, char *argv[])
78 {
79         int retcode;
80         char *slave;
81         gid_t gid;
82         struct group *grp;
83
84         retcode = EX_OK;
85
86         if ((slave = ptsname(STDIN_FILENO)) == NULL)
87                 retcode = EX_USAGE;
88         else {
89                 gid = (grp = getgrnam("tty")) ? grp->gr_gid : -1;
90                 if (chown(slave, getuid(), gid) == 0 &&
91                     chmod(slave, S_IRUSR | S_IWUSR | S_IWGRP) == 0)
92                         retcode = 0;
93                 else
94                         retcode = EX_NOPERM;
95         }
96
97         /*
98          * grantpt(3) checks the retcode for being either zero or
99          * nonzero.  Any nonzero return results in errno being set
100          * to EACCES.
101          */
102         exit(retcode);
103 }