*** empty log message ***
[glibc.git] / sysdeps / mach / hurd / access.c
blobeb57d92f27dd2ed1bb67eac4c74c6d97f4f588a0
1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
19 #include <ansidecl.h>
20 #include <unistd.h>
21 #include <hurd.h>
22 #include <hurd/port.h>
23 #include <hurd/id.h>
24 #include <hurd/lookup.h>
25 #include <fcntl.h>
27 /* Test for access to FILE by our real user and group IDs. */
28 int
29 DEFUN(__access, (file, type), CONST char *file AND int type)
31 error_t err;
32 file_t rcrdir, rcwdir, io;
33 int flags, allowed;
35 error_t reauthenticate (int which, file_t *result)
37 /* Get a port to our root directory, authenticated with the real IDs. */
38 error_t err;
39 mach_port_t ref;
40 ref = __mach_reply_port ();
41 err = HURD_PORT_USE
42 (&_hurd_ports[which],
44 err = __io_reauthenticate (port, ref, MACH_MSG_TYPE_MAKE_SEND);
45 if (!err)
46 err = __auth_user_authenticate (_hurd_id.rid_auth,
47 ref, MACH_MSG_TYPE_MAKE_SEND,
48 result);
49 err;
50 }));
51 __mach_port_destroy (__mach_task_self (), ref);
52 return err;
55 error_t init_port (int which, error_t (*operate) (mach_port_t))
57 switch (which)
59 case INIT_PORT_AUTH:
60 return (*operate) (_hurd_id.rid_auth);
61 case INIT_PORT_CRDIR:
62 return (reauthenticate (INIT_PORT_CRDIR, &rcrdir) ?:
63 (*operate) (rcrdir));
64 case INIT_PORT_CWDIR:
65 return (reauthenticate (INIT_PORT_CWDIR, &rcwdir) ?:
66 (*operate) (rcwdir));
67 default:
68 return _hurd_ports_use (which, operate);
72 rcrdir = rcwdir = MACH_PORT_NULL;
74 HURD_CRITICAL_BEGIN;
76 __mutex_lock (&_hurd_id.lock);
77 /* Get _hurd_id up to date. */
78 if (err = _hurd_check_ids ())
79 goto lose;
81 if (_hurd_id.rid_auth == MACH_PORT_NULL)
83 /* Set up _hurd_id.rid_auth. This is a special auth server port
84 which uses the real uid and gid (the first aux uid and gid) as
85 the only effective uid and gid. */
87 if (_hurd_id.aux.nuids < 1 || _hurd_id.aux.ngids < 1)
89 /* We do not have a real UID and GID. Lose, lose, lose! */
90 err = EGRATUITOUS;
91 goto lose;
94 /* Create a new auth port using our real UID and GID (the first
95 auxiliary UID and GID) as the only effective IDs. */
96 if (err = __USEPORT (AUTH,
97 __auth_makeauth (port,
98 NULL, MACH_MSG_TYPE_COPY_SEND, 0,
99 _hurd_id.aux.uids, 1,
100 _hurd_id.aux.uids,
101 _hurd_id.aux.nuids,
102 _hurd_id.aux.gids, 1,
103 _hurd_id.aux.gids,
104 _hurd_id.aux.ngids,
105 &_hurd_id.rid_auth)))
106 goto lose;
109 if (!err)
110 /* Look up the file name using the modified init ports. */
111 err = __hurd_file_name_lookup (&init_port, &__getdport,
112 file, 0, 0, &io);
114 /* We are done with _hurd_id.rid_auth now. */
115 lose:
116 __mutex_unlock (&_hurd_id.lock);
118 HURD_CRITICAL_END;
120 if (rcrdir != MACH_PORT_NULL)
121 __mach_port_deallocate (__mach_task_self (), rcrdir);
122 if (rcwdir != MACH_PORT_NULL)
123 __mach_port_deallocate (__mach_task_self (), rcwdir);
124 if (err)
125 return __hurd_fail (err);
127 /* Find out what types of access we are allowed to this file. */
128 err = __file_check_access (io, &allowed);
129 __mach_port_deallocate (__mach_task_self (), io);
130 if (err)
131 return __hurd_fail (err);
133 flags = 0;
134 if (type & R_OK)
135 flags |= O_READ;
136 if (type & W_OK)
137 flags |= O_WRITE;
138 if (type & X_OK)
139 flags |= O_EXEC;
141 if (flags & ~allowed)
142 /* We are not allowed all the requested types of access. */
143 return __hurd_fail (EACCES);
145 return 0;
148 weak_alias (__access, access)