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