Back to index

glibc  2.9
setauth.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <hurd.h>
00020 #include <hurd/port.h>
00021 #include <hurd/id.h>
00022 #include "set-hooks.h"
00023 
00024 /* Things in the library which want to be run when the auth port changes.  */
00025 DEFINE_HOOK (_hurd_reauth_hook, (auth_t new_auth));
00026 
00027 #include <cthreads.h>
00028 static struct mutex reauth_lock = MUTEX_INITIALIZER;
00029 
00030 
00031 /* Set the auth port to NEW, and reauthenticate
00032    everything used by the library.  */
00033 error_t
00034 _hurd_setauth (auth_t new)
00035 {
00036   error_t err;
00037   unsigned int d;
00038   mach_port_t newport, ref;
00039 
00040   /* Give the new send right a user reference.
00041      This is a good way to check that it is valid.  */
00042   if (err = __mach_port_mod_refs (__mach_task_self (), new,
00043                               MACH_PORT_RIGHT_SEND, 1))
00044     return err;
00045 
00046   HURD_CRITICAL_BEGIN;
00047 
00048   /* We lock against another thread doing setauth.  Anyone who sets
00049      _hurd_ports[INIT_PORT_AUTH] some other way is asking to lose.  */
00050   __mutex_lock (&reauth_lock);
00051 
00052   /* Install the new port in the cell.  */
00053   __mutex_lock (&_hurd_id.lock);
00054   _hurd_port_set (&_hurd_ports[INIT_PORT_AUTH], new);
00055   _hurd_id.valid = 0;
00056   if (_hurd_id.rid_auth)
00057     {
00058       __mach_port_deallocate (__mach_task_self (), _hurd_id.rid_auth);
00059       _hurd_id.rid_auth = MACH_PORT_NULL;
00060     }
00061   __mutex_unlock (&_hurd_id.lock);
00062 
00063   if (_hurd_init_dtable != NULL)
00064     /* We just have the simple table we got at startup.
00065        Otherwise, a reauth_hook in dtable.c takes care of this.  */
00066     for (d = 0; d < _hurd_init_dtablesize; ++d)
00067       if (_hurd_init_dtable[d] != MACH_PORT_NULL)
00068        {
00069          mach_port_t new;
00070          ref = __mach_reply_port ();
00071          if (! __io_reauthenticate (_hurd_init_dtable[d],
00072                                  ref, MACH_MSG_TYPE_MAKE_SEND) &&
00073              ! HURD_PORT_USE (&_hurd_ports[INIT_PORT_AUTH],
00074                             __auth_user_authenticate
00075                             (port,
00076                             ref, MACH_MSG_TYPE_MAKE_SEND,
00077                             &new)))
00078            {
00079              __mach_port_deallocate (__mach_task_self (),
00080                                   _hurd_init_dtable[d]);
00081              _hurd_init_dtable[d] = new;
00082            }
00083          __mach_port_destroy (__mach_task_self (), ref);
00084        }
00085 
00086   ref = __mach_reply_port ();
00087   if (__USEPORT (CRDIR,
00088                ! __io_reauthenticate (port,
00089                                    ref, MACH_MSG_TYPE_MAKE_SEND) &&
00090                ! __auth_user_authenticate (new,
00091                                         ref, MACH_MSG_TYPE_MAKE_SEND,
00092                                         &newport)))
00093     _hurd_port_set (&_hurd_ports[INIT_PORT_CRDIR], newport);
00094   __mach_port_destroy (__mach_task_self (), ref);
00095 
00096   ref = __mach_reply_port ();
00097   if (__USEPORT (CWDIR,
00098                ! __io_reauthenticate (port,
00099                                    ref, MACH_MSG_TYPE_MAKE_SEND) &&
00100                ! __auth_user_authenticate (new,
00101                                         ref, MACH_MSG_TYPE_MAKE_SEND,
00102                                         &newport)))
00103     _hurd_port_set (&_hurd_ports[INIT_PORT_CWDIR], newport);
00104   __mach_port_destroy (__mach_task_self (), ref);
00105 
00106   /* Run things which want to do reauthorization stuff.  */
00107   RUN_HOOK (_hurd_reauth_hook, (new));
00108 
00109   __mutex_unlock (&reauth_lock);
00110 
00111   HURD_CRITICAL_END;
00112 
00113   return 0;
00114 }
00115 
00116 int
00117 __setauth (auth_t new)
00118 {
00119   error_t err = _hurd_setauth (new);
00120   return err ? __hurd_fail (err) : 0;
00121 }
00122 
00123 weak_alias (__setauth, setauth)