unix_grantpt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include <assert.h>
  24. #include <errno.h>
  25. #include <grp.h>
  26. #include <limits.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/resource.h>
  30. #include <sys/stat.h>
  31. #include <sys/types.h>
  32. #include <sys/wait.h>
  33. #include <unistd.h>
  34. #include "pty-private.h"
  35. /* uClinux-2.0 has vfork, but Linux 2.0 doesn't */
  36. #include <sys/syscall.h>
  37. #if ! defined __NR_vfork
  38. #define vfork fork
  39. #endif
  40. extern int __ptsname_r (int fd, char *buf, size_t buflen) attribute_hidden;
  41. /* Return the result of ptsname_r in the buffer pointed to by PTS,
  42. which should be of length BUF_LEN. If it is too long to fit in
  43. this buffer, a sufficiently long buffer is allocated using malloc,
  44. and returned in PTS. 0 is returned upon success, -1 otherwise. */
  45. static int
  46. pts_name (int fd, char **pts, size_t buf_len)
  47. {
  48. int rv;
  49. char *buf = *pts;
  50. for (;;)
  51. {
  52. char *new_buf;
  53. if (buf_len)
  54. {
  55. rv = __ptsname_r (fd, buf, buf_len);
  56. if (rv != 0 || memchr (buf, '\0', buf_len))
  57. /* We either got an error, or we succeeded and the
  58. returned name fit in the buffer. */
  59. break;
  60. /* Try again with a longer buffer. */
  61. buf_len += buf_len; /* Double it */
  62. }
  63. else
  64. /* No initial buffer; start out by mallocing one. */
  65. buf_len = 128; /* First time guess. */
  66. if (buf != *pts)
  67. /* We've already malloced another buffer at least once. */
  68. new_buf = realloc (buf, buf_len);
  69. else
  70. new_buf = malloc (buf_len);
  71. if (! new_buf)
  72. {
  73. rv = -1;
  74. errno = ENOMEM;
  75. break;
  76. }
  77. buf = new_buf;
  78. }
  79. if (rv == 0)
  80. *pts = buf; /* Return buffer to the user. */
  81. else if (buf != *pts)
  82. free (buf); /* Free what we malloced when returning an error. */
  83. return rv;
  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
  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. if (pts_name (fd, &buf, sizeof (_buf)))
  103. return -1;
  104. if (__stat(buf, &st) < 0)
  105. goto cleanup;
  106. /* Make sure that we own the device. */
  107. uid = getuid ();
  108. if (st.st_uid != uid)
  109. {
  110. if (chown (buf, uid, st.st_gid) < 0)
  111. goto helper;
  112. }
  113. gid = getgid ();
  114. /* Make sure the group of the device is that special group. */
  115. if (st.st_gid != gid)
  116. {
  117. if (chown (buf, uid, gid) < 0)
  118. goto helper;
  119. }
  120. /* Make sure the permission mode is set to readable and writable by
  121. the owner, and writable by the group. */
  122. if ((st.st_mode & ACCESSPERMS) != (S_IRUSR|S_IWUSR|S_IWGRP))
  123. {
  124. if (chmod (buf, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
  125. goto helper;
  126. }
  127. retval = 0;
  128. goto cleanup;
  129. /* We have to use the helper program. */
  130. helper:
  131. pid = vfork ();
  132. if (pid == -1)
  133. goto cleanup;
  134. else if (pid == 0)
  135. {
  136. /* Disable core dumps. */
  137. struct rlimit rl = { 0, 0 };
  138. setrlimit (RLIMIT_CORE, &rl);
  139. /* We pase the master pseudo terminal as file descriptor PTY_FILENO. */
  140. if (fd != PTY_FILENO)
  141. if (dup2 (fd, PTY_FILENO) < 0)
  142. _exit (FAIL_EBADF);
  143. execle (_PATH_PT_CHOWN, _PATH_PT_CHOWN, NULL, NULL);
  144. _exit (FAIL_EXEC);
  145. }
  146. else
  147. {
  148. int w;
  149. if (waitpid (pid, &w, 0) == -1)
  150. goto cleanup;
  151. if (!WIFEXITED (w))
  152. errno = ENOEXEC;
  153. else
  154. switch (WEXITSTATUS(w))
  155. {
  156. case 0:
  157. retval = 0;
  158. break;
  159. case FAIL_EBADF:
  160. errno = EBADF;
  161. break;
  162. case FAIL_EINVAL:
  163. errno = EINVAL;
  164. break;
  165. case FAIL_EACCES:
  166. errno = EACCES;
  167. break;
  168. case FAIL_EXEC:
  169. errno = ENOEXEC;
  170. break;
  171. default:
  172. assert(! "getpt: internal error: invalid exit code from pt_chown");
  173. }
  174. }
  175. cleanup:
  176. if (buf != _buf)
  177. free (buf);
  178. return retval;
  179. }