unix_grantpt.c 4.7 KB

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