valgrind: do leak checking, and exit with code 1 on error (not 0)
[gnulib/ericb.git] / lib / pt_chown.c
blobccc04fd88af87453709fe25bc7d7c84ed3bc1c2b
1 /* pt_chown - helper program for `grantpt'.
2 Copyright (C) 1998-1999, 2009-2011 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 <http://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include <errno.h>
21 #include <grp.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
28 #include "pty-private.h"
30 /* For security reasons, we try to minimize the dependencies on libraries
31 outside libc. This means, in particular:
32 - No use of gettext(), since it's usually implemented in libintl.
33 - No use of error() or argp, since they rely on gettext by default. */
36 static int
37 do_pt_chown (void)
39 char *pty;
40 struct stat st;
41 struct group *p;
42 gid_t gid;
44 /* Check that PTY_FILENO is a valid master pseudo terminal. */
45 pty = ptsname (PTY_FILENO);
46 if (pty == NULL)
47 return errno == EBADF ? FAIL_EBADF : FAIL_EINVAL;
49 /* Check that the returned slave pseudo terminal is a
50 character device. */
51 if (stat (pty, &st) < 0 || !S_ISCHR (st.st_mode))
52 return FAIL_EINVAL;
54 /* Get the group ID of the special `tty' group. */
55 p = getgrnam (TTY_GROUP);
56 gid = p ? p->gr_gid : getgid ();
58 /* Set the owner to the real user ID, and the group to that special
59 group ID. */
60 if (st.st_gid != gid && chown (pty, getuid (), gid) < 0)
61 return FAIL_EACCES;
63 /* Set the permission mode to readable and writable by the owner,
64 and writable by the group. */
65 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != (S_IRUSR|S_IWUSR|S_IWGRP)
66 && chmod (pty, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
67 return FAIL_EACCES;
69 return 0;
73 int
74 main (int argc, char *argv[])
76 uid_t euid = geteuid ();
78 if (argc == 1 && euid == 0)
80 /* Normal invocation of this program is with no arguments and
81 with privileges. */
82 return do_pt_chown ();
85 /* It would be possible to drop setuid/setgid privileges here. But it is not
86 really needed, since the code below only calls strcmp and [f]printf. */
89 int do_help = 0;
90 int do_version = 0;
91 int remaining;
93 for (remaining = 1; remaining < argc; remaining++)
95 const char *arg = argv[remaining];
97 if (arg[0] == '-')
99 if (strcmp (arg, "--") == 0)
101 remaining++;
102 break;
104 else if (strcmp (arg, "--help") == 0)
105 do_help = 1;
106 else if (strcmp (arg, "--version") == 0)
107 do_version = 1;
108 else
110 fprintf (stderr, "pt_chown: invalid option: %s\n", arg);
111 return EXIT_FAILURE;
114 else
115 break;
118 if (remaining < argc)
120 fprintf (stderr, "pt_chown: too many arguments\n");
121 return EXIT_FAILURE;
124 if (do_help)
126 printf ("Usage: pt_chown [OPTION...]\n");
127 printf ("Set the owner, group and access permission of the slave pseudo terminal\n"
128 "corresponding to the master pseudo terminal passed on file descriptor %d.\n"
129 "This is the helper program for the 'grantpt' function. It is not intended\n"
130 "to be run directly from the command line.\n",
131 PTY_FILENO);
132 printf ("\n");
133 printf (" --help Give this help list\n");
134 printf (" --version Print program version\n");
135 printf ("\n");
136 printf ("The owner is set to the current user, the group is set to '%s', and the\n"
137 "access permission is set to '%o'.\n",
138 TTY_GROUP, S_IRUSR|S_IWUSR|S_IWGRP);
139 printf ("Please report bugs to <bug-gnulib@gnu.org>.\n");
140 return EXIT_SUCCESS;
143 if (do_version)
145 printf ("pt_chown (GNU %s) %s\n", "libc", "2.11");
146 printf ("Copyright (C) %s Free Software Foundation, Inc.\n"
147 "This is free software; see the source for copying conditions. There is NO\n"
148 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
149 "1999");
150 return EXIT_SUCCESS;
154 /* Check if we are properly installed. */
155 if (euid != 0)
157 fprintf (stderr, "pt_chown: needs to be installed setuid 'root'\n");
158 return FAIL_EXEC;
161 return EXIT_SUCCESS;