ced7032dd1e1d9bb99f151d259972082a4b118f2
[platform/upstream/linaro-glibc.git] / login / programs / pt_chown.c
1 /* pt_chmod - helper program for `grantpt'.
2    Copyright (C) 1998, 1999 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.
5
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.
10
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.
15
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.  */
20
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
33 #include "pty-private.h"
34
35 /* Get libc version number.  */
36 #include "../version.h"
37
38 #define PACKAGE _libc_intl_domainname
39
40 /* Name and version of program.  */
41 static void print_version (FILE *stream, struct argp_state *state);
42 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
43
44 /* Function to print some extra text in the help message.  */
45 static char *more_help (int key, const char *text, void *input);
46
47 /* Data structure to communicate with argp functions.  */
48 static struct argp argp =
49 {
50   NULL, NULL, NULL, NULL, NULL, more_help
51 };
52
53
54 /* Print the version information.  */
55 static void
56 print_version (FILE *stream, struct argp_state *state)
57 {
58   fprintf (stream, "pt_chown (GNU %s) %s\n", PACKAGE, VERSION);
59   fprintf (stream, gettext ("\
60 Copyright (C) %s Free Software Foundation, Inc.\n\
61 This is free software; see the source for copying conditions.  There is NO\n\
62 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
63 "), "1999");
64 }
65
66 static char *
67 more_help (int key, const char *text, void *input)
68 {
69   char *cp;
70
71   switch (key)
72     {
73     case ARGP_KEY_HELP_PRE_DOC:
74       asprintf (&cp, gettext ("\
75 Set the owner, group and access permission of the slave pseudo\
76  terminal corresponding to the master pseudo terminal passed on\
77  file descriptor `%d'.  This is the helper program for the\
78  `grantpt' function.  It is not intended to be run directly from\
79  the command line.\n"),
80                 PTY_FILENO);
81       return cp;
82     case ARGP_KEY_HELP_EXTRA:
83       /* We print some extra information.  */
84       asprintf (&cp, gettext ("\
85 The owner is set to the current user, the group is set to `%s',\
86  and the access permission is set to `%o'.\n\n\
87 %s"),
88                 TTY_GROUP, S_IRUSR|S_IWUSR|S_IWGRP, gettext ("\
89 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
90       return cp;
91     default:
92       break;
93     }
94   return (char *) text;
95 }
96
97 static int
98 do_pt_chown (void)
99 {
100   char *pty;
101   struct stat st;
102   struct group *p;
103   gid_t gid;
104
105   /* Check that PTY_FILENO is a valid master pseudo terminal.  */
106   pty = ptsname (PTY_FILENO);
107   if (pty == NULL)
108     return errno == EBADF ? FAIL_EBADF : FAIL_EINVAL;
109
110   /* Check that the returned slave pseudo terminal is a
111      character device.  */
112   if (stat (pty, &st) < 0 || !S_ISCHR(st.st_mode))
113     return FAIL_EINVAL;
114
115   /* Get the group ID of the special `tty' group.  */
116   p = getgrnam (TTY_GROUP);
117   gid = p ? p->gr_gid : getgid ();
118
119   /* Set the owner to the real user ID, and the group to that special
120      group ID.  */
121   if (chown (pty, getuid (), gid) < 0)
122     return FAIL_EACCES;
123
124   /* Set the permission mode to readable and writable by the owner,
125      and writable by the group.  */
126   if (chmod (pty, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
127     return FAIL_EACCES;
128
129   return 0;
130 }
131
132
133 int
134 main (int argc, char *argv[])
135 {
136   uid_t euid = geteuid ();
137   int remaining;
138
139   /* Normal invocation of this program is with no arguments and
140      with privileges.
141      FIXME: Should use capable (CAP_CHOWN|CAP_FOWNER).  */
142   if (argc == 1 && euid == 0)
143     return do_pt_chown ();
144
145   /* We aren't going to be using privileges, so drop them right now. */
146   setuid (getuid ());
147
148   /* Set locale via LC_ALL.  */
149   setlocale (LC_ALL, "");
150
151   /* Set the text message domain.  */
152   textdomain (PACKAGE);
153
154   /* parse and process arguments.  */
155   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
156
157   if (remaining < argc)
158     {
159       /* We should not be called with any non-option parameters.  */
160       error (0, 0, gettext ("too many arguments"));
161       argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
162                  program_invocation_short_name);
163       return EXIT_FAILURE;
164     }
165
166   /* Check if we are properly installed.  */
167   if (euid != 0)
168     error (FAIL_EXEC, 0, gettext ("needs to be installed setuid `root'"));
169
170   return EXIT_SUCCESS;
171 }