setauth.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <hurd.h>
  15. #include <hurd/port.h>
  16. #include <hurd/id.h>
  17. #include <hurdlock.h>
  18. #include "set-hooks.h"
  19. /* Things in the library which want to be run when the auth port changes. */
  20. DEFINE_HOOK (_hurd_reauth_hook, (auth_t new_auth));
  21. static unsigned int reauth_lock = LLL_LOCK_INITIALIZER;
  22. /* Set the auth port to NEW, and reauthenticate
  23. everything used by the library. */
  24. error_t
  25. _hurd_setauth (auth_t new)
  26. {
  27. error_t err;
  28. unsigned int d;
  29. mach_port_t newport, ref;
  30. /* Give the new send right a user reference.
  31. This is a good way to check that it is valid. */
  32. if (err = __mach_port_mod_refs (__mach_task_self (), new,
  33. MACH_PORT_RIGHT_SEND, 1))
  34. return err;
  35. HURD_CRITICAL_BEGIN;
  36. /* We lock against another thread doing setauth. Anyone who sets
  37. _hurd_ports[INIT_PORT_AUTH] some other way is asking to lose. */
  38. __mutex_lock (&reauth_lock);
  39. /* Install the new port in the cell. */
  40. __mutex_lock (&_hurd_id.lock);
  41. _hurd_port_set (&_hurd_ports[INIT_PORT_AUTH], new);
  42. _hurd_id.valid = 0;
  43. if (_hurd_id.rid_auth)
  44. {
  45. __mach_port_deallocate (__mach_task_self (), _hurd_id.rid_auth);
  46. _hurd_id.rid_auth = MACH_PORT_NULL;
  47. }
  48. __mutex_unlock (&_hurd_id.lock);
  49. if (_hurd_init_dtable != NULL)
  50. /* We just have the simple table we got at startup.
  51. Otherwise, a reauth_hook in dtable.c takes care of this. */
  52. for (d = 0; d < _hurd_init_dtablesize; ++d)
  53. if (_hurd_init_dtable[d] != MACH_PORT_NULL)
  54. {
  55. mach_port_t new;
  56. ref = __mach_reply_port ();
  57. if (! __io_reauthenticate (_hurd_init_dtable[d],
  58. ref, MACH_MSG_TYPE_MAKE_SEND)
  59. && ! HURD_PORT_USE (&_hurd_ports[INIT_PORT_AUTH],
  60. __auth_user_authenticate
  61. (port,
  62. ref, MACH_MSG_TYPE_MAKE_SEND,
  63. &new)))
  64. {
  65. __mach_port_deallocate (__mach_task_self (),
  66. _hurd_init_dtable[d]);
  67. _hurd_init_dtable[d] = new;
  68. }
  69. __mach_port_destroy (__mach_task_self (), ref);
  70. }
  71. ref = __mach_reply_port ();
  72. if (__USEPORT (CRDIR,
  73. ! __io_reauthenticate (port,
  74. ref, MACH_MSG_TYPE_MAKE_SEND)
  75. && ! __auth_user_authenticate (new,
  76. ref, MACH_MSG_TYPE_MAKE_SEND,
  77. &newport)))
  78. _hurd_port_set (&_hurd_ports[INIT_PORT_CRDIR], newport);
  79. __mach_port_destroy (__mach_task_self (), ref);
  80. ref = __mach_reply_port ();
  81. if (__USEPORT (CWDIR,
  82. ! __io_reauthenticate (port,
  83. ref, MACH_MSG_TYPE_MAKE_SEND)
  84. && ! __auth_user_authenticate (new,
  85. ref, MACH_MSG_TYPE_MAKE_SEND,
  86. &newport)))
  87. _hurd_port_set (&_hurd_ports[INIT_PORT_CWDIR], newport);
  88. __mach_port_destroy (__mach_task_self (), ref);
  89. /* Run things which want to do reauthorization stuff. */
  90. RUN_HOOK (_hurd_reauth_hook, (new));
  91. __mutex_unlock (&reauth_lock);
  92. HURD_CRITICAL_END;
  93. return 0;
  94. }
  95. int
  96. __setauth (auth_t new)
  97. {
  98. error_t err = _hurd_setauth (new);
  99. return err ? __hurd_fail (err) : 0;
  100. }
  101. weak_alias (__setauth, setauth)