unix_grantpt.c 4.8 KB

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