1 /* pt_chown - helper program for 'grantpt'.
2 Copyright (C) 1998-1999, 2009-2018 Free Software Foundation, Inc.
3 Contributed by C. Scott Ananian <cananian@alumni.princeton.edu>, 1998.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
30 #include "pty-private.h"
32 /* For security reasons, we try to minimize the dependencies on libraries
33 outside libc. This means, in particular:
34 - No use of gettext(), since it's usually implemented in libintl.
35 - No use of error() or argp, since they rely on gettext by default. */
46 /* Check that PTY_FILENO is a valid master pseudo terminal. */
47 pty
= ptsname (PTY_FILENO
);
49 return errno
== EBADF
? FAIL_EBADF
: FAIL_EINVAL
;
51 /* Check that the returned slave pseudo terminal is a
53 if (stat (pty
, &st
) < 0 || !S_ISCHR (st
.st_mode
))
56 /* Get the group ID of the special 'tty' group. */
57 p
= getgrnam (TTY_GROUP
);
58 gid
= p
? p
->gr_gid
: getgid ();
60 /* Set the owner to the real user ID, and the group to that special
62 if (st
.st_gid
!= gid
&& chown (pty
, getuid (), gid
) < 0)
65 /* Set the permission mode to readable and writable by the owner,
66 and writable by the group. */
67 if ((st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
)) != (S_IRUSR
|S_IWUSR
|S_IWGRP
)
68 && chmod (pty
, S_IRUSR
|S_IWUSR
|S_IWGRP
) < 0)
76 main (int argc
, char *argv
[])
78 uid_t euid
= geteuid ();
80 if (argc
== 1 && euid
== ROOT_UID
)
82 /* Normal invocation of this program is with no arguments and
84 return do_pt_chown ();
87 /* It would be possible to drop setuid/setgid privileges here. But it is not
88 really needed, since the code below only calls strcmp and [f]printf. */
95 for (remaining
= 1; remaining
< argc
; remaining
++)
97 const char *arg
= argv
[remaining
];
101 if (strcmp (arg
, "--") == 0)
106 else if (strcmp (arg
, "--help") == 0)
108 else if (strcmp (arg
, "--version") == 0)
112 fprintf (stderr
, "pt_chown: invalid option: %s\n", arg
);
120 if (remaining
< argc
)
122 fprintf (stderr
, "pt_chown: too many arguments\n");
128 printf ("Usage: pt_chown [OPTION...]\n");
129 printf ("Set the owner, group and access permission of the slave pseudo terminal\n"
130 "corresponding to the master pseudo terminal passed on file descriptor %d.\n"
131 "This is the helper program for the 'grantpt' function. It is not intended\n"
132 "to be run directly from the command line.\n",
135 printf (" --help Give this help list\n");
136 printf (" --version Print program version\n");
138 printf ("The owner is set to the current user, the group is set to '%s', and the\n"
139 "access permission is set to '%o'.\n",
140 TTY_GROUP
, S_IRUSR
|S_IWUSR
|S_IWGRP
);
141 printf ("Please report bugs to <bug-gnulib@gnu.org>.\n");
147 printf ("pt_chown (GNU %s) %s\n", "libc", "2.11");
148 printf ("Copyright (C) %s Free Software Foundation, Inc.\n"
149 "This is free software; see the source for copying conditions. There is NO\n"
150 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
156 /* Check if we are properly installed. */
157 if (euid
!= ROOT_UID
)
159 fprintf (stderr
, "pt_chown: needs to be installed setuid 'root'\n");