unix_grantpt.c 4.5 KB

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