This source file includes following definitions.
- do_pt_chown
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <config.h>
19
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>
27
28 #include "root-uid.h"
29
30 #include "pty-private.h"
31
32
33
34
35
36
37
38 static int
39 do_pt_chown (void)
40 {
41 char *pty;
42 struct stat st;
43 struct group *p;
44 gid_t gid;
45
46
47 pty = ptsname (PTY_FILENO);
48 if (pty == NULL)
49 return errno == EBADF ? FAIL_EBADF : FAIL_EINVAL;
50
51
52
53 if (stat (pty, &st) < 0 || !S_ISCHR (st.st_mode))
54 return FAIL_EINVAL;
55
56
57 p = getgrnam (TTY_GROUP);
58 gid = p ? p->gr_gid : getgid ();
59
60
61
62 if (st.st_gid != gid && chown (pty, getuid (), gid) < 0)
63 return FAIL_EACCES;
64
65
66
67 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != (S_IRUSR|S_IWUSR|S_IWGRP)
68 && chmod (pty, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
69 return FAIL_EACCES;
70
71 return 0;
72 }
73
74
75 int
76 main (int argc, char *argv[])
77 {
78 uid_t euid = geteuid ();
79
80 if (argc == 1 && euid == ROOT_UID)
81 {
82
83
84 return do_pt_chown ();
85 }
86
87
88
89
90 {
91 int do_help = 0;
92 int do_version = 0;
93 int remaining;
94
95 for (remaining = 1; remaining < argc; remaining++)
96 {
97 const char *arg = argv[remaining];
98
99 if (arg[0] == '-')
100 {
101 if (strcmp (arg, "--") == 0)
102 {
103 remaining++;
104 break;
105 }
106 else if (strcmp (arg, "--help") == 0)
107 do_help = 1;
108 else if (strcmp (arg, "--version") == 0)
109 do_version = 1;
110 else
111 {
112 fprintf (stderr, "pt_chown: invalid option: %s\n", arg);
113 return EXIT_FAILURE;
114 }
115 }
116 else
117 break;
118 }
119
120 if (remaining < argc)
121 {
122 fprintf (stderr, "pt_chown: too many arguments\n");
123 return EXIT_FAILURE;
124 }
125
126 if (do_help)
127 {
128 printf ("Usage: pt_chown [OPTION...]\n");
129 printf ("Set the owner, group and access permission of the slave pseudo terminal\n"
130 "corresponding to the master pseudo terminal passed on file descriptor %d.\n"
131 "This is the helper program for the 'grantpt' function. It is not intended\n"
132 "to be run directly from the command line.\n",
133 PTY_FILENO);
134 printf ("\n");
135 printf (" --help Give this help list\n");
136 printf (" --version Print program version\n");
137 printf ("\n");
138 printf ("The owner is set to the current user, the group is set to '%s', and the\n"
139 "access permission is set to '%o'.\n",
140 TTY_GROUP, S_IRUSR|S_IWUSR|S_IWGRP);
141 printf ("Please report bugs to <bug-gnulib@gnu.org>.\n");
142 return EXIT_SUCCESS;
143 }
144
145 if (do_version)
146 {
147 printf ("pt_chown (GNU %s) %s\n", "libc", "2.11");
148 printf ("Copyright (C) %s Free Software Foundation, Inc.\n"
149 "This is free software; see the source for copying conditions. There is NO\n"
150 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
151 "1999");
152 return EXIT_SUCCESS;
153 }
154 }
155
156
157 if (euid != ROOT_UID)
158 {
159 fprintf (stderr, "pt_chown: needs to be installed setuid 'root'\n");
160 return FAIL_EXEC;
161 }
162
163 return EXIT_SUCCESS;
164 }