home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / hurd / __setauth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-04  |  3.0 KB  |  107 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  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.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <hurd.h>
  20. #include <gnu-stabs.h>
  21.  
  22. /* Things in the library which want to be run when the auth port changes.  */
  23. const struct
  24.   {
  25.     size_t n;
  26.     void (*fn[0]) (auth_t new_auth);
  27.   } _hurd_reauth_hook;
  28.  
  29.  
  30. static struct mutex reauth_lock;
  31.  
  32.  
  33. /* Set the auth port to NEW, and reauthenticate
  34.    everything used by the library.  */
  35. int
  36. __setauth (auth_t new)
  37. {
  38.   error_t err;
  39.   auth_t old;
  40.   int d;
  41.   mach_port_t newport;
  42.   void (**fn) (auth_t);
  43.  
  44.   /* Give the new send right a user reference.
  45.      This is a good way to check that it is valid.  */
  46.   if (__mach_port_mod_refs (__mach_task_self (), new,
  47.                 MACH_PORT_RIGHT_SEND, 1))
  48.     {
  49.       errno = EINVAL;
  50.       return -1;
  51.     }
  52.  
  53.   /* We lock against another thread doing setauth.
  54.      Anyone who sets _hurd_auth some other way is asking to lose.  */
  55.   __mutex_lock (&reauth_lock);
  56.  
  57.   /* Install the new port in the _hurd_auth cell.  */
  58.   __mutex_lock (&_hurd_idlock);
  59.   _hurd_port_set (&_hurd_auth, new);
  60.   _hurd_id_valid = 0;
  61.   __mutex_unlock (&_hurd_idlock);
  62.  
  63.   if (_hurd_init_dtable != NULL)
  64.     /* We just have the simple table we got at startup.
  65.        Otherwise, a reauth_hook in dtable.c takes care of this.  */
  66.     for (d = 0; d < _hurd_init_dtablesize; ++d)
  67.       if (_hurd_init_dtable[d] != MACH_PORT_NULL)
  68.     {
  69.       mach_port_t new;
  70.       if (! __io_reauthenticate (_hurd_init_dtable[d]) &&
  71.           ! _HURD_PORT_USE (&_hurd_auth,
  72.                 __auth_user_authenticate (_hurd_init_dtable[d],
  73.                               &new)))
  74.         {
  75.           __mach_port_deallocate (__mach_task_self (),
  76.                       _hurd_init_dtable[d]);
  77.           _hurd_init_dtable[d] = new;
  78.         }
  79.     }
  80.  
  81.   if (_HURD_PORT_USE (&_hurd_crdir,
  82.               ! __io_reauthenticate (port) &&
  83.               ! __auth_user_authenticate (new, port, &newport)))
  84.     _hurd_port_set (&_hurd_crdir, newport);
  85.  
  86.   if (_HURD_PORT_USE (&_hurd_cwdir,
  87.               ! __io_reauthenticate (port) &&
  88.               ! __auth_user_authenticate (new, port, &newport)))
  89.     _hurd_port_set (&_hurd_cwdir, newport);
  90.  
  91.   /* Run things which want to do reauthorization stuff.  */
  92.   for (fn = _hurd_reauth_hook.fn; *fn != NULL; ++fn)
  93.     (**fn) (new);
  94.  
  95.   __mutex_unlock (&reauth_lock);
  96.  
  97.   return 0;
  98. }
  99.  
  100. static void
  101. init_reauth (void)
  102. {
  103.   __mutex_init (&reauth_lock);
  104. }
  105.  
  106. text_set_element (__libc_subinit, init_reauth);
  107.