]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/access.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / mach / hurd / access.c
1 /* Copyright (C) 1991-2018 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #include <unistd.h>
19 #include <hurd.h>
20 #include <hurd/port.h>
21 #include <hurd/id.h>
22 #include <hurd/lookup.h>
23 #include <fcntl.h>
24
25 static int
26 hurd_fail_seterrno (error_t err)
27 {
28 return __hurd_fail (err);
29 }
30
31 static int
32 hurd_fail_noerrno (error_t err)
33 {
34 return -1;
35 }
36
37 static int
38 access_common (const char *file, int type, int (*errfunc) (error_t))
39 {
40 error_t err;
41 file_t rcrdir, rcwdir, io;
42 int flags, allowed;
43
44 error_t reauthenticate (int which, file_t *result)
45 {
46 /* Get a port to our root directory, authenticated with the real IDs. */
47 error_t err;
48 mach_port_t ref;
49 ref = __mach_reply_port ();
50 err = HURD_PORT_USE
51 (&_hurd_ports[which],
52 ({
53 err = __io_reauthenticate (port, ref, MACH_MSG_TYPE_MAKE_SEND);
54 if (!err)
55 err = __auth_user_authenticate (_hurd_id.rid_auth,
56 ref, MACH_MSG_TYPE_MAKE_SEND,
57 result);
58 err;
59 }));
60 __mach_port_destroy (__mach_task_self (), ref);
61 return err;
62 }
63
64 error_t init_port (int which, error_t (*operate) (mach_port_t))
65 {
66 switch (which)
67 {
68 case INIT_PORT_AUTH:
69 return (*operate) (_hurd_id.rid_auth);
70 case INIT_PORT_CRDIR:
71 return (reauthenticate (INIT_PORT_CRDIR, &rcrdir) ?:
72 (*operate) (rcrdir));
73 case INIT_PORT_CWDIR:
74 return (reauthenticate (INIT_PORT_CWDIR, &rcwdir) ?:
75 (*operate) (rcwdir));
76 default:
77 return _hurd_ports_use (which, operate);
78 }
79 }
80
81 rcrdir = rcwdir = MACH_PORT_NULL;
82
83 HURD_CRITICAL_BEGIN;
84
85 __mutex_lock (&_hurd_id.lock);
86 /* Get _hurd_id up to date. */
87 if (err = _hurd_check_ids ())
88 goto lose;
89
90 if (_hurd_id.rid_auth == MACH_PORT_NULL)
91 {
92 /* Set up _hurd_id.rid_auth. This is a special auth server port
93 which uses the real uid and gid (the first aux uid and gid) as
94 the only effective uid and gid. */
95
96 if (_hurd_id.aux.nuids < 1 || _hurd_id.aux.ngids < 1)
97 {
98 /* We do not have a real UID and GID. Lose, lose, lose! */
99 err = EGRATUITOUS;
100 goto lose;
101 }
102
103 /* Create a new auth port using our real UID and GID (the first
104 auxiliary UID and GID) as the only effective IDs. */
105 if (err = __USEPORT (AUTH,
106 __auth_makeauth (port,
107 NULL, MACH_MSG_TYPE_COPY_SEND, 0,
108 _hurd_id.aux.uids, 1,
109 _hurd_id.aux.uids,
110 _hurd_id.aux.nuids,
111 _hurd_id.aux.gids, 1,
112 _hurd_id.aux.gids,
113 _hurd_id.aux.ngids,
114 &_hurd_id.rid_auth)))
115 goto lose;
116 }
117
118 if (!err)
119 /* Look up the file name using the modified init ports. */
120 err = __hurd_file_name_lookup (&init_port, &__getdport, 0,
121 file, 0, 0, &io);
122
123 /* We are done with _hurd_id.rid_auth now. */
124 lose:
125 __mutex_unlock (&_hurd_id.lock);
126
127 HURD_CRITICAL_END;
128
129 if (rcrdir != MACH_PORT_NULL)
130 __mach_port_deallocate (__mach_task_self (), rcrdir);
131 if (rcwdir != MACH_PORT_NULL)
132 __mach_port_deallocate (__mach_task_self (), rcwdir);
133 if (err)
134 return errfunc (err);
135
136 /* Find out what types of access we are allowed to this file. */
137 err = __file_check_access (io, &allowed);
138 __mach_port_deallocate (__mach_task_self (), io);
139 if (err)
140 return errfunc (err);
141
142 flags = 0;
143 if (type & R_OK)
144 flags |= O_READ;
145 if (type & W_OK)
146 flags |= O_WRITE;
147 if (type & X_OK)
148 flags |= O_EXEC;
149
150 if (flags & ~allowed)
151 /* We are not allowed all the requested types of access. */
152 return errfunc (EACCES);
153
154 return 0;
155 }
156
157 /* Test for access to FILE by our real user and group IDs without setting
158 errno. This may be unsafe to run during initialization of tunables
159 since access_common calls __hurd_file_name_lookup, which calls
160 __hurd_file_name_lookup_retry, which can set errno. */
161 int
162 __access_noerrno (const char *file, int type)
163 {
164 return access_common (file, type, hurd_fail_noerrno);
165 }
166
167 /* Test for access to FILE by our real user and group IDs. */
168 int
169 __access (const char *file, int type)
170 {
171 return access_common (file, type, hurd_fail_seterrno);
172 }
173 weak_alias (__access, access)