Blame hurd/setauth.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/port.h>
Packit 6c4009
#include <hurd/id.h>
Packit 6c4009
#include <hurdlock.h>
Packit 6c4009
#include "set-hooks.h"
Packit 6c4009
Packit 6c4009
/* Things in the library which want to be run when the auth port changes.  */
Packit 6c4009
DEFINE_HOOK (_hurd_reauth_hook, (auth_t new_auth));
Packit 6c4009
Packit 6c4009
static unsigned int reauth_lock = LLL_INITIALIZER;
Packit 6c4009
Packit 6c4009
/* Set the auth port to NEW, and reauthenticate
Packit 6c4009
   everything used by the library.  */
Packit 6c4009
error_t
Packit 6c4009
_hurd_setauth (auth_t new)
Packit 6c4009
{
Packit 6c4009
  error_t err;
Packit 6c4009
  unsigned int d;
Packit 6c4009
  mach_port_t newport, ref;
Packit 6c4009
Packit 6c4009
  /* Give the new send right a user reference.
Packit 6c4009
     This is a good way to check that it is valid.  */
Packit 6c4009
  if (err = __mach_port_mod_refs (__mach_task_self (), new,
Packit 6c4009
				  MACH_PORT_RIGHT_SEND, 1))
Packit 6c4009
    return err;
Packit 6c4009
Packit 6c4009
  HURD_CRITICAL_BEGIN;
Packit 6c4009
Packit 6c4009
  /* We lock against another thread doing setauth.  Anyone who sets
Packit 6c4009
     _hurd_ports[INIT_PORT_AUTH] some other way is asking to lose.  */
Packit 6c4009
  __mutex_lock (&reauth_lock);
Packit 6c4009
Packit 6c4009
  /* Install the new port in the cell.  */
Packit 6c4009
  __mutex_lock (&_hurd_id.lock);
Packit 6c4009
  _hurd_port_set (&_hurd_ports[INIT_PORT_AUTH], new);
Packit 6c4009
  _hurd_id.valid = 0;
Packit 6c4009
  if (_hurd_id.rid_auth)
Packit 6c4009
    {
Packit 6c4009
      __mach_port_deallocate (__mach_task_self (), _hurd_id.rid_auth);
Packit 6c4009
      _hurd_id.rid_auth = MACH_PORT_NULL;
Packit 6c4009
    }
Packit 6c4009
  __mutex_unlock (&_hurd_id.lock);
Packit 6c4009
Packit 6c4009
  if (_hurd_init_dtable != NULL)
Packit 6c4009
    /* We just have the simple table we got at startup.
Packit 6c4009
       Otherwise, a reauth_hook in dtable.c takes care of this.  */
Packit 6c4009
    for (d = 0; d < _hurd_init_dtablesize; ++d)
Packit 6c4009
      if (_hurd_init_dtable[d] != MACH_PORT_NULL)
Packit 6c4009
	{
Packit 6c4009
	  mach_port_t new;
Packit 6c4009
	  ref = __mach_reply_port ();
Packit 6c4009
	  if (! __io_reauthenticate (_hurd_init_dtable[d],
Packit 6c4009
				     ref, MACH_MSG_TYPE_MAKE_SEND) &&
Packit 6c4009
	      ! HURD_PORT_USE (&_hurd_ports[INIT_PORT_AUTH],
Packit 6c4009
			       __auth_user_authenticate
Packit 6c4009
			       (port,
Packit 6c4009
				ref, MACH_MSG_TYPE_MAKE_SEND,
Packit 6c4009
				&new)))
Packit 6c4009
	    {
Packit 6c4009
	      __mach_port_deallocate (__mach_task_self (),
Packit 6c4009
				      _hurd_init_dtable[d]);
Packit 6c4009
	      _hurd_init_dtable[d] = new;
Packit 6c4009
	    }
Packit 6c4009
	  __mach_port_destroy (__mach_task_self (), ref);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
  ref = __mach_reply_port ();
Packit 6c4009
  if (__USEPORT (CRDIR,
Packit 6c4009
		 ! __io_reauthenticate (port,
Packit 6c4009
					ref, MACH_MSG_TYPE_MAKE_SEND) &&
Packit 6c4009
		 ! __auth_user_authenticate (new,
Packit 6c4009
					     ref, MACH_MSG_TYPE_MAKE_SEND,
Packit 6c4009
					     &newport)))
Packit 6c4009
    _hurd_port_set (&_hurd_ports[INIT_PORT_CRDIR], newport);
Packit 6c4009
  __mach_port_destroy (__mach_task_self (), ref);
Packit 6c4009
Packit 6c4009
  ref = __mach_reply_port ();
Packit 6c4009
  if (__USEPORT (CWDIR,
Packit 6c4009
		 ! __io_reauthenticate (port,
Packit 6c4009
					ref, MACH_MSG_TYPE_MAKE_SEND) &&
Packit 6c4009
		 ! __auth_user_authenticate (new,
Packit 6c4009
					     ref, MACH_MSG_TYPE_MAKE_SEND,
Packit 6c4009
					     &newport)))
Packit 6c4009
    _hurd_port_set (&_hurd_ports[INIT_PORT_CWDIR], newport);
Packit 6c4009
  __mach_port_destroy (__mach_task_self (), ref);
Packit 6c4009
Packit 6c4009
  /* Run things which want to do reauthorization stuff.  */
Packit 6c4009
  RUN_HOOK (_hurd_reauth_hook, (new));
Packit 6c4009
Packit 6c4009
  __mutex_unlock (&reauth_lock);
Packit 6c4009
Packit 6c4009
  HURD_CRITICAL_END;
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__setauth (auth_t new)
Packit 6c4009
{
Packit 6c4009
  error_t err = _hurd_setauth (new);
Packit 6c4009
  return err ? __hurd_fail (err) : 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__setauth, setauth)