unix_grantpt.c 4.4 KB

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