pt_chown.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* pt_chmod - helper program for `grantpt'.
  2. Copyright (C) 1998-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the 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. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <argp.h>
  16. #include <errno.h>
  17. #include <error.h>
  18. #include <grp.h>
  19. #include <libintl.h>
  20. #include <locale.h>
  21. #include <signal.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #ifdef HAVE_LIBCAP
  28. # include <sys/capability.h>
  29. # include <sys/prctl.h>
  30. #endif
  31. #include "pty-private.h"
  32. /* Get libc version number. */
  33. #include "../version.h"
  34. #define PACKAGE _libc_intl_domainname
  35. /* Name and version of program. */
  36. static void print_version (FILE *stream, struct argp_state *state);
  37. void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
  38. /* Function to print some extra text in the help message. */
  39. static char *more_help (int key, const char *text, void *input);
  40. /* Data structure to communicate with argp functions. */
  41. static struct argp argp =
  42. {
  43. NULL, NULL, NULL, NULL, NULL, more_help
  44. };
  45. /* Print the version information. */
  46. static void
  47. print_version (FILE *stream, struct argp_state *state)
  48. {
  49. fprintf (stream, "pt_chown %s%s\n", PKGVERSION, VERSION);
  50. fprintf (stream, gettext ("\
  51. Copyright (C) %s Free Software Foundation, Inc.\n\
  52. This is free software; see the source for copying conditions. There is NO\n\
  53. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
  54. "), "2024");
  55. }
  56. static char *
  57. more_help (int key, const char *text, void *input)
  58. {
  59. char *cp;
  60. char *tp;
  61. switch (key)
  62. {
  63. case ARGP_KEY_HELP_PRE_DOC:
  64. asprintf (&cp, gettext ("\
  65. Set the owner, group and access permission of the slave pseudo\
  66. terminal corresponding to the master pseudo terminal passed on\
  67. file descriptor `%d'. This is the helper program for the\
  68. `grantpt' function. It is not intended to be run directly from\
  69. the command line.\n"),
  70. PTY_FILENO);
  71. return cp;
  72. case ARGP_KEY_HELP_EXTRA:
  73. /* We print some extra information. */
  74. if (asprintf (&tp, gettext ("\
  75. For bug reporting instructions, please see:\n\
  76. %s.\n"), REPORT_BUGS_TO) < 0)
  77. return NULL;
  78. if (asprintf (&cp, gettext ("\
  79. The owner is set to the current user, the group is set to `%s',\
  80. and the access permission is set to `%o'.\n\n\
  81. %s"),
  82. TTY_GROUP, S_IRUSR|S_IWUSR|S_IWGRP, tp) < 0)
  83. {
  84. free (tp);
  85. return NULL;
  86. }
  87. return cp;
  88. default:
  89. break;
  90. }
  91. return (char *) text;
  92. }
  93. static int
  94. do_pt_chown (void)
  95. {
  96. char *pty;
  97. struct stat64 st;
  98. struct group *p;
  99. gid_t gid;
  100. /* Check that PTY_FILENO is a valid master pseudo terminal. */
  101. pty = ptsname (PTY_FILENO);
  102. if (pty == NULL)
  103. return errno == EBADF ? FAIL_EBADF : FAIL_EINVAL;
  104. /* Check that the returned slave pseudo terminal is a
  105. character device. */
  106. if (stat64 (pty, &st) < 0 || !S_ISCHR (st.st_mode))
  107. return FAIL_EINVAL;
  108. /* Get the group ID of the special `tty' group. */
  109. p = getgrnam (TTY_GROUP);
  110. gid = p ? p->gr_gid : getgid ();
  111. /* Set the owner to the real user ID, and the group to that special
  112. group ID. */
  113. if (chown (pty, getuid (), gid) < 0)
  114. return FAIL_EACCES;
  115. /* Set the permission mode to readable and writable by the owner,
  116. and writable by the group. */
  117. if ((st.st_mode & ACCESSPERMS) != (S_IRUSR|S_IWUSR|S_IWGRP)
  118. && chmod (pty, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
  119. return FAIL_EACCES;
  120. return 0;
  121. }
  122. int
  123. main (int argc, char *argv[])
  124. {
  125. uid_t euid = geteuid ();
  126. uid_t uid = getuid ();
  127. int remaining;
  128. sigset_t signalset;
  129. /* Clear any signal mask from the parent process. */
  130. sigemptyset (&signalset);
  131. sigprocmask (SIG_SETMASK, &signalset, NULL);
  132. if (argc == 1 && euid == 0)
  133. {
  134. #ifdef HAVE_LIBCAP
  135. /* Drop privileges. */
  136. if (uid != euid)
  137. {
  138. static const cap_value_t cap_list[] =
  139. { CAP_CHOWN, CAP_FOWNER };
  140. # define ncap_list (sizeof (cap_list) / sizeof (cap_list[0]))
  141. cap_t caps = cap_init ();
  142. if (caps == NULL)
  143. return FAIL_ENOMEM;
  144. /* There is no reason why these should not work. */
  145. cap_set_flag (caps, CAP_PERMITTED, ncap_list, cap_list, CAP_SET);
  146. cap_set_flag (caps, CAP_EFFECTIVE, ncap_list, cap_list, CAP_SET);
  147. int res = cap_set_proc (caps);
  148. cap_free (caps);
  149. if (__glibc_unlikely (res != 0))
  150. return FAIL_EXEC;
  151. }
  152. #endif
  153. /* Normal invocation of this program is with no arguments and
  154. with privileges. */
  155. return do_pt_chown ();
  156. }
  157. /* We aren't going to be using privileges, so drop them right now. */
  158. setuid (uid);
  159. /* Set locale via LC_ALL. */
  160. setlocale (LC_ALL, "");
  161. /* Set the text message domain. */
  162. textdomain (PACKAGE);
  163. /* parse and process arguments. */
  164. argp_parse (&argp, argc, argv, 0, &remaining, NULL);
  165. if (remaining < argc)
  166. {
  167. /* We should not be called with any non-option parameters. */
  168. error (0, 0, gettext ("too many arguments"));
  169. argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
  170. program_invocation_short_name);
  171. return EXIT_FAILURE;
  172. }
  173. /* Check if we are properly installed. */
  174. if (euid != 0)
  175. error (FAIL_EXEC, 0, gettext ("needs to be installed setuid `root'"));
  176. return EXIT_SUCCESS;
  177. }