unix_grantpt.c 4.7 KB

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