Update.
[glibc.git] / login / programs / pt_chown.c
blobee176068f809148132b480a3008579e2bdd32635
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.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 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>
33 #include "pty-private.h"
35 /* Get libc version number. */
36 #include "../version.h"
38 #define PACKAGE _libc_intl_domainname
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;
44 /* Function to print some extra text in the help message. */
45 static char *more_help (int key, const char *text, void *input);
47 /* Data structure to communicate with argp functions. */
48 static struct argp argp =
50 NULL, NULL, NULL, NULL, NULL, more_help
54 /* Print the version information. */
55 static void
56 print_version (FILE *stream, struct argp_state *state)
58 fprintf (stream, "pt_chmod (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");
66 static char *
67 more_help (int key, const char *text, void *input)
69 char *cp;
71 switch (key)
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;
94 return (char *) text;
97 int
98 main (int argc, char *argv[])
100 char *pty;
101 int remaining;
102 struct stat st;
103 struct group *p;
104 gid_t gid;
106 /* Set locale via LC_ALL. */
107 setlocale (LC_ALL, "");
109 /* Set the text message domain. */
110 textdomain (PACKAGE);
112 /* parse and process arguments. */
113 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
115 if (remaining < argc)
117 /* We should not be called with any non-option parameters. */
118 error (0, 0, gettext ("too many arguments"));
119 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
120 program_invocation_short_name);
121 exit (EXIT_FAILURE);
124 /* Check if we are properly installed. */
125 if (geteuid () != 0)
126 error (FAIL_EXEC, 0, gettext ("needs to be installed setuid `root'"));
128 /* Check that PTY_FILENO is a valid master pseudo terminal. */
129 pty = ptsname (PTY_FILENO);
130 if (pty == NULL)
131 return errno == EBADF ? FAIL_EBADF : FAIL_EINVAL;
132 close (PTY_FILENO);
134 /* Check that the returned slave pseudo terminal is a
135 character device. */
136 if (stat (pty, &st) < 0 || !S_ISCHR(st.st_mode))
137 return FAIL_EINVAL;
139 /* Get the group ID of the special `tty' group. */
140 p = getgrnam (TTY_GROUP);
141 gid = p ? p->gr_gid : getgid ();
143 /* Set the owner to the real user ID, and the group to that special
144 group ID. */
145 if (chown (pty, getuid (), gid) < 0)
146 return FAIL_EACCES;
148 /* Set the permission mode to readable and writable by the owner,
149 and writable by the group. */
150 if (chmod (pty, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
151 return FAIL_EACCES;
153 exit (EXIT_SUCCESS);