1 /* Copyright (C) 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library 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 GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 see <http://www.gnu.org/licenses/>. */
18
19 #include <assert.h>
20 #include <errno.h>
21 #include <grp.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/resource.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include "pty-private.h"
31
32
33 /* Return the result of ptsname_r in the buffer pointed to by PTS,
34 which should be of length BUF_LEN. If it is too long to fit in
35 this buffer, a sufficiently long buffer is allocated using malloc,
36 and returned in PTS. 0 is returned upon success, -1 otherwise. */
37 static int
pts_name(int fd,char ** pts,size_t buf_len)38 pts_name (int fd, char **pts, size_t buf_len)
39 {
40 int rv;
41 char *buf = *pts;
42
43 for (;;)
44 {
45 char *new_buf;
46
47 if (buf_len)
48 {
49 rv = ptsname_r (fd, buf, buf_len);
50
51 if (rv != 0 || memchr (buf, '\0', buf_len))
52 /* We either got an error, or we succeeded and the
53 returned name fit in the buffer. */
54 break;
55
56 /* Try again with a longer buffer. */
57 buf_len += buf_len; /* Double it */
58 }
59 else
60 /* No initial buffer; start out by mallocing one. */
61 buf_len = 128; /* First time guess. */
62
63 if (buf != *pts)
64 /* We've already malloced another buffer at least once. */
65 new_buf = realloc (buf, buf_len);
66 else
67 new_buf = malloc (buf_len);
68 if (! new_buf)
69 {
70 rv = -1;
71 /* __set_errno(ENOMEM); */
72 break;
73 }
74 buf = new_buf;
75 }
76
77 if (rv == 0)
78 *pts = buf; /* Return buffer to the user. */
79 else if (buf != *pts)
80 free (buf); /* Free what we malloced when returning an error. */
81
82 return rv;
83 }
84
85 /* Change the ownership and access permission of the slave pseudo
86 terminal associated with the master pseudo terminal specified
87 by FD. */
88 int
grantpt(int fd)89 grantpt (int fd)
90 {
91 int retval = -1;
92 #ifdef PATH_MAX
93 char _buf[PATH_MAX];
94 #else
95 char _buf[512];
96 #endif
97 char *buf = _buf;
98 struct stat st;
99 uid_t uid;
100 gid_t gid;
101 pid_t pid;
102
103 if (pts_name (fd, &buf, sizeof (_buf)))
104 return -1;
105
106 if (stat(buf, &st) < 0)
107 goto cleanup;
108
109 /* Make sure that we own the device. */
110 uid = getuid ();
111 if (st.st_uid != uid)
112 {
113 if (chown (buf, uid, st.st_gid) < 0)
114 goto helper;
115 }
116
117 gid = getgid ();
118
119 /* Make sure the group of the device is that special group. */
120 if (st.st_gid != gid)
121 {
122 if (chown (buf, uid, gid) < 0)
123 goto helper;
124 }
125
126 /* Make sure the permission mode is set to readable and writable by
127 the owner, and writable by the group. */
128 if ((st.st_mode & ACCESSPERMS) != (S_IRUSR|S_IWUSR|S_IWGRP))
129 {
130 if (chmod (buf, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
131 goto helper;
132 }
133
134 retval = 0;
135 goto cleanup;
136
137 /* We have to use the helper program. */
138 helper:
139
140 pid = vfork ();
141 if (pid == -1)
142 goto cleanup;
143 else if (pid == 0)
144 {
145 /* Disable core dumps. */
146 struct rlimit rl = { 0, 0 };
147 setrlimit (RLIMIT_CORE, &rl);
148
149 /* We pase the master pseudo terminal as file descriptor PTY_FILENO. */
150 if (fd != PTY_FILENO)
151 if (dup2 (fd, PTY_FILENO) < 0)
152 _exit (FAIL_EBADF);
153
154 execle (_PATH_PT_CHOWN, _PATH_PT_CHOWN, NULL, NULL);
155 _exit (FAIL_EXEC);
156 }
157 else
158 {
159 int w;
160
161 if (waitpid (pid, &w, 0) == -1)
162 goto cleanup;
163 if (!WIFEXITED (w))
164 errno = ENOEXEC;
165 else
166 switch (WEXITSTATUS(w))
167 {
168 case 0:
169 retval = 0;
170 break;
171 case FAIL_EBADF:
172 errno = EBADF;
173 break;
174 case FAIL_EINVAL:
175 errno = EINVAL;
176 break;
177 case FAIL_EACCES:
178 errno = EACCES;
179 break;
180 case FAIL_EXEC:
181 errno = ENOEXEC;
182 break;
183
184 default:
185 assert(! "getpt: internal error: invalid exit code from pt_chown");
186 }
187 }
188
189 cleanup:
190 if (buf != _buf)
191 free (buf);
192
193 return retval;
194 }
195