Replace FSF snail mail address with URLs.
[glibc.git] / login / programs / pt_chown.c
blobfe98964c33f9fbc0680bdf44dfeadb797075a959
1 /* pt_chmod - helper program for `grantpt'.
2 Copyright (C) 1998, 1999, 2009, 2012 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, see
18 <http://www.gnu.org/licenses/>. */
20 #include <argp.h>
21 #include <errno.h>
22 #include <error.h>
23 #include <grp.h>
24 #include <libintl.h>
25 #include <locale.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #ifdef HAVE_LIBCAP
32 # include <sys/capability.h>
33 # include <sys/prctl.h>
34 #endif
36 #include "pty-private.h"
38 /* Get libc version number. */
39 #include "../version.h"
41 #define PACKAGE _libc_intl_domainname
43 /* Name and version of program. */
44 static void print_version (FILE *stream, struct argp_state *state);
45 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
47 /* Function to print some extra text in the help message. */
48 static char *more_help (int key, const char *text, void *input);
50 /* Data structure to communicate with argp functions. */
51 static struct argp argp =
53 NULL, NULL, NULL, NULL, NULL, more_help
57 /* Print the version information. */
58 static void
59 print_version (FILE *stream, struct argp_state *state)
61 fprintf (stream, "pt_chown (GNU %s) %s\n", PACKAGE, VERSION);
62 fprintf (stream, gettext ("\
63 Copyright (C) %s Free Software Foundation, Inc.\n\
64 This is free software; see the source for copying conditions. There is NO\n\
65 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
66 "), "2012");
69 static char *
70 more_help (int key, const char *text, void *input)
72 char *cp;
74 switch (key)
76 case ARGP_KEY_HELP_PRE_DOC:
77 asprintf (&cp, gettext ("\
78 Set the owner, group and access permission of the slave pseudo\
79 terminal corresponding to the master pseudo terminal passed on\
80 file descriptor `%d'. This is the helper program for the\
81 `grantpt' function. It is not intended to be run directly from\
82 the command line.\n"),
83 PTY_FILENO);
84 return cp;
85 case ARGP_KEY_HELP_EXTRA:
86 /* We print some extra information. */
87 asprintf (&cp, gettext ("\
88 The owner is set to the current user, the group is set to `%s',\
89 and the access permission is set to `%o'.\n\n\
90 %s"),
91 TTY_GROUP, S_IRUSR|S_IWUSR|S_IWGRP, gettext ("\
92 For bug reporting instructions, please see:\n\
93 <http://www.gnu.org/software/libc/bugs.html>.\n"));
94 return cp;
95 default:
96 break;
98 return (char *) text;
101 static int
102 do_pt_chown (void)
104 char *pty;
105 struct stat64 st;
106 struct group *p;
107 gid_t gid;
109 /* Check that PTY_FILENO is a valid master pseudo terminal. */
110 pty = ptsname (PTY_FILENO);
111 if (pty == NULL)
112 return errno == EBADF ? FAIL_EBADF : FAIL_EINVAL;
114 /* Check that the returned slave pseudo terminal is a
115 character device. */
116 if (stat64 (pty, &st) < 0 || !S_ISCHR (st.st_mode))
117 return FAIL_EINVAL;
119 /* Get the group ID of the special `tty' group. */
120 p = getgrnam (TTY_GROUP);
121 gid = p ? p->gr_gid : getgid ();
123 /* Set the owner to the real user ID, and the group to that special
124 group ID. */
125 if (chown (pty, getuid (), gid) < 0)
126 return FAIL_EACCES;
128 /* Set the permission mode to readable and writable by the owner,
129 and writable by the group. */
130 if ((st.st_mode & ACCESSPERMS) != (S_IRUSR|S_IWUSR|S_IWGRP)
131 && chmod (pty, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
132 return FAIL_EACCES;
134 return 0;
139 main (int argc, char *argv[])
141 uid_t euid = geteuid ();
142 uid_t uid = getuid ();
143 int remaining;
145 if (argc == 1 && euid == 0)
147 #ifdef HAVE_LIBCAP
148 /* Drop privileges. */
149 if (uid != euid)
151 static const cap_value_t cap_list[] =
152 { CAP_CHOWN, CAP_FOWNER };
153 # define ncap_list (sizeof (cap_list) / sizeof (cap_list[0]))
154 cap_t caps = cap_init ();
155 if (caps == NULL)
156 return FAIL_ENOMEM;
158 /* There is no reason why these should not work. */
159 cap_set_flag (caps, CAP_PERMITTED, ncap_list, cap_list, CAP_SET);
160 cap_set_flag (caps, CAP_EFFECTIVE, ncap_list, cap_list, CAP_SET);
162 int res = cap_set_proc (caps);
164 cap_free (caps);
166 if (__builtin_expect (res != 0, 0))
167 return FAIL_EXEC;
169 #endif
171 /* Normal invocation of this program is with no arguments and
172 with privileges. */
173 return do_pt_chown ();
176 /* We aren't going to be using privileges, so drop them right now. */
177 setuid (uid);
179 /* Set locale via LC_ALL. */
180 setlocale (LC_ALL, "");
182 /* Set the text message domain. */
183 textdomain (PACKAGE);
185 /* parse and process arguments. */
186 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
188 if (remaining < argc)
190 /* We should not be called with any non-option parameters. */
191 error (0, 0, gettext ("too many arguments"));
192 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
193 program_invocation_short_name);
194 return EXIT_FAILURE;
197 /* Check if we are properly installed. */
198 if (euid != 0)
199 error (FAIL_EXEC, 0, gettext ("needs to be installed setuid `root'"));
201 return EXIT_SUCCESS;