home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / sysdeps / mach / hurd / __access.c next >
Encoding:
C/C++ Source or Header  |  1992-03-21  |  2.9 KB  |  99 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 <ansidecl.h>
  20. #include <unistd.h>
  21. #include <hurd.h>
  22.  
  23. /* Test for access to FILE by our real user and group IDs.  */
  24. int
  25. DEFUN(__access, (file, type), CONST char *file AND int type)
  26. {
  27.   error_t err;
  28.   file_t crdir, cwdir, rcrdir, rcwdir, io;
  29.   int dealloc_crdir, dealloc_cwdir;
  30.   int flags;
  31.  
  32.   /* Set up _hurd_rid_auth.  */
  33.   __mutex_lock (&_hurd_idlock);
  34.   if (!_hurd_id_valid)
  35.     {
  36.       if (_hurd_rid_auth != MACH_PORT_NULL)
  37.     {
  38.       __mach_port_deallocate (__mach_task_self (), _hurd_rid_auth);
  39.       _hurd_rid_auth = MACH_PORT_NULL;
  40.     }
  41.       if (err = __auth_getids (_hurd_auth, &_hurd_id))
  42.     goto lose;
  43.  
  44.     }
  45.   if (_hurd_rid_auth == MACH_PORT_NULL)
  46.     {
  47.       idblock_t rid;
  48.       rid = _hurd_id;
  49.       /* XXX Should keep supplementary ids or not? */
  50.       rid.uids[0] = rid.ruid;
  51.       rid.gids[0] = rid.rgid;
  52.       rid.ruid = _hurd_id.uids[0];
  53.       rid.rgid = _hurd_id.gids[0];
  54.       if (err = __auth_makeauth (_hurd_auth, &rid, &_hurd_rid_auth))
  55.     goto lose;
  56.     }
  57.  
  58.   crdir = _hurd_port_get (&_hurd_crdir, &dealloc_crdir);
  59.   err = __io_reauthenticate (crdir);
  60.   if (!err)
  61.     {
  62.       err = __auth_user_authenticate (_hurd_rid_auth, crdir, &rcrdir);
  63.       __mach_port_deallocate (__mach_task_self (), crdir);
  64.     }
  65.   _hurd_port_free (crdir, &dealloc_crdir);
  66.   cwdir = _hurd_port_get (&_hurd_cwdir, &dealloc_cwdir);
  67.   err = __io_reauthenticate (cwdir);
  68.   if (!err)
  69.     {
  70.       err = __auth_user_authenticate (_hurd_rid_auth, cwdir, &rcwdir);
  71.       __mach_port_deallocate (__mach_task_self (), cwdir);
  72.     }
  73.   __mutex_unlock (&_hurd_idlock);
  74.   _hurd_port_free (cwdir, &dealloc_cwdir);
  75.   if (err)
  76.     return __hurd_fail (err);
  77.  
  78.   flags = 0;
  79.   if (type & R_OK)
  80.     flags |= FS_LOOKUP_READ;
  81.   if (type & W_OK)
  82.     flags |= FS_LOOKUP_WRITE;
  83.   if (type & X_OK)
  84.     flags |= FS_LOOKUP_EXECUTE;
  85.  
  86.   err = __hurd_path_lookup (rcrdir, rcwdir, file, flags, 0, &io);
  87.   __mach_port_deallocate (__mach_task_self (), rcrdir);
  88.   __mach_port_deallocate (__mach_task_self (), rcwdir);
  89.   if (err)
  90.     return __hurd_fail (err);
  91.  
  92.   __mach_port_deallocate (__mach_task_self (), io);
  93.   return 0;
  94.  
  95.  lose:
  96.   __mutex_unlock (&_hurd_idlock);
  97.   return __hurd_fail (err);
  98. }
  99.