home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / sysdeps / mach / hurd / __access.c next >
Encoding:
C/C++ Source or Header  |  1994-08-30  |  4.2 KB  |  137 lines

  1. /* Copyright (C) 1991, 1992, 1993, 1994 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 <ansidecl.h>
  20. #include <unistd.h>
  21. #include <hurd.h>
  22. #include <hurd/port.h>
  23. #include <hurd/id.h>
  24. #include <fcntl.h>
  25.  
  26. /* Test for access to FILE by our real user and group IDs.  */
  27. int
  28. DEFUN(__access, (file, type), CONST char *file AND int type)
  29. {
  30.   error_t err;
  31.   file_t crdir, cwdir, rcrdir, rcwdir, io;
  32.   struct hurd_userlink crdir_ulink, cwdir_ulink;
  33.   int flags, allowed;
  34.   mach_port_t ref;
  35.  
  36.   HURD_CRITICAL_BEGIN;
  37.  
  38.   __mutex_lock (&_hurd_id.lock);
  39.   /* Get _hurd_id up to date.  */
  40.   if (err = _hurd_check_ids ())
  41.     goto lose;
  42.  
  43.   if (_hurd_id.rid_auth == MACH_PORT_NULL)
  44.     {
  45.       /* Set up _hurd_id.rid_auth.  This is a special auth server port
  46.      which uses the real uid and gid (the first aux uid and gid) as
  47.      the only effective uid and gid.  */
  48.  
  49.       if (_hurd_id.aux.nuids < 1 || _hurd_id.aux.ngids < 1)
  50.     {
  51.       /* We do not have a real UID and GID.  Lose, lose, lose!  */
  52.       err = EGRATUITOUS;
  53.       goto lose;
  54.     }
  55.  
  56.       /* Create a new auth port using our real UID and GID (the first
  57.      auxiliary UID and GID) as the only effective IDs.  */
  58.       if (err = __USEPORT (AUTH,
  59.                __auth_makeauth (port,
  60.                         NULL, MACH_MSG_TYPE_COPY_SEND, 0,
  61.                         _hurd_id.aux.uids, 1,
  62.                         _hurd_id.aux.gids, 1,
  63.                         _hurd_id.aux.uids,
  64.                         _hurd_id.aux.nuids,
  65.                         _hurd_id.aux.gids,
  66.                         _hurd_id.aux.ngids,
  67.                         &_hurd_id.rid_auth)))
  68.     goto lose;
  69.     }
  70.  
  71.   /* Get a port to our root directory, authenticated with the real IDs.  */
  72.   crdir = _hurd_port_get (&_hurd_ports[INIT_PORT_CRDIR], &crdir_ulink);
  73.   ref = __mach_reply_port ();
  74.   err = __io_reauthenticate (crdir, ref, MACH_MSG_TYPE_MAKE_SEND);
  75.   if (!err)
  76.     err = __auth_user_authenticate (_hurd_id.rid_auth,
  77.                     crdir,
  78.                     ref, MACH_MSG_TYPE_MAKE_SEND,
  79.                     &rcrdir);
  80.   _hurd_port_free (&_hurd_ports[INIT_PORT_CRDIR], &crdir_ulink, crdir);
  81.   __mach_port_destroy (__mach_task_self (), ref);
  82.  
  83.   if (!err)
  84.     {
  85.       /* Get a port to our current working directory, authenticated with
  86.          the real IDs.  */
  87.       cwdir = _hurd_port_get (&_hurd_ports[INIT_PORT_CWDIR], &cwdir_ulink);
  88.       ref = __mach_reply_port ();
  89.       err = __io_reauthenticate (cwdir, ref, MACH_MSG_TYPE_MAKE_SEND);
  90.       if (!err)
  91.     err = __auth_user_authenticate (_hurd_id.rid_auth,
  92.                     cwdir,
  93.                     ref, MACH_MSG_TYPE_MAKE_SEND,
  94.                     &rcwdir);
  95.       _hurd_port_free (&_hurd_ports[INIT_PORT_CWDIR], &cwdir_ulink, cwdir);
  96.       __mach_port_destroy (__mach_task_self (), ref);
  97.     }
  98.  
  99.   /* We are done with _hurd_id.rid_auth now.  */
  100.  lose:
  101.   __mutex_unlock (&_hurd_id.lock);
  102.  
  103.   HURD_CRITICAL_END;
  104.  
  105.   if (err)
  106.     return __hurd_fail (err);
  107.  
  108.   /* Now do a path lookup on FILE, using the crdir and cwdir
  109.      reauthenticated with _hurd_id.rid_auth.  */
  110.  
  111.   err = __hurd_file_name_lookup (rcrdir, rcwdir, file, 0, 0, &io);
  112.   __mach_port_deallocate (__mach_task_self (), rcrdir);
  113.   __mach_port_deallocate (__mach_task_self (), rcwdir);
  114.   if (err)
  115.     return __hurd_fail (err);
  116.  
  117.   /* Find out what types of access we are allowed to this file.  */
  118.   err = __file_check_access (io, &allowed);
  119.   __mach_port_deallocate (__mach_task_self (), io);
  120.   if (err)
  121.     return __hurd_fail (err);
  122.  
  123.   flags = 0;
  124.   if (type & R_OK)
  125.     flags |= O_READ;
  126.   if (type & W_OK)
  127.     flags |= O_WRITE;
  128.   if (type & X_OK)
  129.     flags |= O_EXEC;
  130.  
  131.   if (flags & ~allowed)
  132.     /* We are not allowed all the requested types of access.  */
  133.     return __hurd_fail (EACCES);
  134.  
  135.   return 0;
  136. }
  137.