unix_grantpt.c 4.9 KB

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