Don't override --enable-multi-arch.
[glibc.git] / login / programs / pt_chown.c
blob4c36f2ceac7d5408d255c39f25efe989942a4a2d
1 /* pt_chmod - helper program for `grantpt'.
2 Copyright (C) 1998, 1999, 2009 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by C. Scott Ananian <cananian@alumni.princeton.edu>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <argp.h>
22 #include <errno.h>
23 #include <error.h>
24 #include <grp.h>
25 #include <libintl.h>
26 #include <locale.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #ifdef HAVE_LIBCAP
33 # include <sys/capability.h>
34 # include <sys/prctl.h>
35 #endif
37 #include "pty-private.h"
39 /* Get libc version number. */
40 #include "../version.h"
42 #define PACKAGE _libc_intl_domainname
44 /* Name and version of program. */
45 static void print_version (FILE *stream, struct argp_state *state);
46 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
48 /* Function to print some extra text in the help message. */
49 static char *more_help (int key, const char *text, void *input);
51 /* Data structure to communicate with argp functions. */
52 static struct argp argp =
54 NULL, NULL, NULL, NULL, NULL, more_help
58 /* Print the version information. */
59 static void
60 print_version (FILE *stream, struct argp_state *state)
62 fprintf (stream, "pt_chown (GNU %s) %s\n", PACKAGE, VERSION);
63 fprintf (stream, gettext ("\
64 Copyright (C) %s Free Software Foundation, Inc.\n\
65 This is free software; see the source for copying conditions. There is NO\n\
66 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
67 "), "1999");
70 static char *
71 more_help (int key, const char *text, void *input)
73 char *cp;
75 switch (key)
77 case ARGP_KEY_HELP_PRE_DOC:
78 asprintf (&cp, gettext ("\
79 Set the owner, group and access permission of the slave pseudo\
80 terminal corresponding to the master pseudo terminal passed on\
81 file descriptor `%d'. This is the helper program for the\
82 `grantpt' function. It is not intended to be run directly from\
83 the command line.\n"),
84 PTY_FILENO);
85 return cp;
86 case ARGP_KEY_HELP_EXTRA:
87 /* We print some extra information. */
88 asprintf (&cp, gettext ("\
89 The owner is set to the current user, the group is set to `%s',\
90 and the access permission is set to `%o'.\n\n\
91 %s"),
92 TTY_GROUP, S_IRUSR|S_IWUSR|S_IWGRP, gettext ("\
93 For bug reporting instructions, please see:\n\
94 <http://www.gnu.org/software/libc/bugs.html>.\n"));
95 return cp;
96 default:
97 break;
99 return (char *) text;
102 static int
103 do_pt_chown (void)
105 char *pty;
106 struct stat64 st;
107 struct group *p;
108 gid_t gid;
110 /* Check that PTY_FILENO is a valid master pseudo terminal. */
111 pty = ptsname (PTY_FILENO);
112 if (pty == NULL)
113 return errno == EBADF ? FAIL_EBADF : FAIL_EINVAL;
115 /* Check that the returned slave pseudo terminal is a
116 character device. */
117 if (stat64 (pty, &st) < 0 || !S_ISCHR (st.st_mode))
118 return FAIL_EINVAL;
120 /* Get the group ID of the special `tty' group. */
121 p = getgrnam (TTY_GROUP);
122 gid = p ? p->gr_gid : getgid ();
124 /* Set the owner to the real user ID, and the group to that special
125 group ID. */
126 if (st.st_gid != gid && chown (pty, getuid (), gid) < 0)
127 return FAIL_EACCES;
129 /* Set the permission mode to readable and writable by the owner,
130 and writable by the group. */
131 if ((st.st_mode & ACCESSPERMS) != (S_IRUSR|S_IWUSR|S_IWGRP)
132 && chmod (pty, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
133 return FAIL_EACCES;
135 return 0;
140 main (int argc, char *argv[])
142 uid_t euid = geteuid ();
143 uid_t uid = getuid ();
144 int remaining;
146 if (argc == 1 && euid == 0)
148 #ifdef HAVE_LIBCAP
149 /* Drop privileges. */
150 if (uid != euid)
152 static const cap_value_t cap_list[] =
153 { CAP_CHOWN, CAP_FOWNER };
154 # define ncap_list (sizeof (cap_list) / sizeof (cap_list[0]))
155 cap_t caps = cap_init ();
156 if (caps == NULL)
157 return FAIL_ENOMEM;
159 /* There is no reason why these should not work. */
160 cap_set_flag (caps, CAP_PERMITTED, ncap_list, cap_list, CAP_SET);
161 cap_set_flag (caps, CAP_EFFECTIVE, ncap_list, cap_list, CAP_SET);
163 int res = cap_set_proc (caps);
165 cap_free (caps);
167 if (__builtin_expect (res != 0, 0))
168 return FAIL_EXEC;
170 #endif
172 /* Normal invocation of this program is with no arguments and
173 with privileges. */
174 return do_pt_chown ();
177 /* We aren't going to be using privileges, so drop them right now. */
178 setuid (uid);
180 /* Set locale via LC_ALL. */
181 setlocale (LC_ALL, "");
183 /* Set the text message domain. */
184 textdomain (PACKAGE);
186 /* parse and process arguments. */
187 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
189 if (remaining < argc)
191 /* We should not be called with any non-option parameters. */
192 error (0, 0, gettext ("too many arguments"));
193 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
194 program_invocation_short_name);
195 return EXIT_FAILURE;
198 /* Check if we are properly installed. */
199 if (euid != 0)
200 error (FAIL_EXEC, 0, gettext ("needs to be installed setuid `root'"));
202 return EXIT_SUCCESS;