summaryrefslogtreecommitdiff
path: root/sysdeps/mach/hurd/faccessat.c
blob: 9f5f1dd4d821a9021ad6bd3f951f7038e5366e2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Test for access to file, relative to open directory.  Hurd version.
   Copyright (C) 2006-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <unistd.h>
#include <sys/types.h>
#include <hurd.h>
#include <hurd/fd.h>
#include <hurd/port.h>
#include <hurd/id.h>
#include <hurd/lookup.h>

static int
hurd_fail_seterrno (error_t err)
{
  return __hurd_fail (err);
}

static int
hurd_fail_noerrno (error_t err)
{
  return -1;
}

static int
__faccessat_common (int fd, const char *file, int type, int at_flags,
                    int (*errfunc) (error_t))
{
  error_t err;
  file_t rcrdir, rcwdir, io;
  int flags, allowed;

  if ((at_flags & AT_EACCESS) == AT_EACCESS)
    {
      /* Use effective permissions.  */
      io = __file_name_lookup_at (fd, at_flags &~ AT_EACCESS, file, 0, 0);
      if (io == MACH_PORT_NULL)
	return -1;
    }
  else
    {
      /* We have to use real permissions instead of the
         usual effective permissions.  */

      int hurd_flags = 0;
      err = __hurd_at_flags (&at_flags, &hurd_flags);
      if (err)
	return errfunc (err);

      error_t reauthenticate_cwdir_at (file_t *result)
	{
	  /* Get a port to the FD directory, authenticated with the real IDs.  */
	  error_t err;
	  mach_port_t ref;
	  ref = __mach_reply_port ();
	  err = HURD_DPORT_USE
	    (fd,
	     ({
	       err = __io_reauthenticate (port, ref, MACH_MSG_TYPE_MAKE_SEND);
	       if (!err)
		 err = __auth_user_authenticate (_hurd_id.rid_auth,
						 ref, MACH_MSG_TYPE_MAKE_SEND,
						 result);
	       err;
	     }));
	  __mach_port_destroy (__mach_task_self (), ref);
	  return err;
	}

      error_t reauthenticate (int which, file_t *result)
	{
	  /* Get a port to our root directory, authenticated with the real IDs.  */
	  error_t err;
	  mach_port_t ref;
	  ref = __mach_reply_port ();
	  err = HURD_PORT_USE
	    (&_hurd_ports[which],
	     ({
	       err = __io_reauthenticate (port, ref, MACH_MSG_TYPE_MAKE_SEND);
	       if (!err)
		 err = __auth_user_authenticate (_hurd_id.rid_auth,
						 ref, MACH_MSG_TYPE_MAKE_SEND,
						 result);
	       err;
	     }));
	  __mach_port_destroy (__mach_task_self (), ref);
	  return err;
	}

      error_t init_port (int which, error_t (*operate) (mach_port_t))
	{
	  switch (which)
	    {
	    case INIT_PORT_AUTH:
	      return (*operate) (_hurd_id.rid_auth);
	    case INIT_PORT_CRDIR:
	      return (reauthenticate (INIT_PORT_CRDIR, &rcrdir) ?:
		      (*operate) (rcrdir));
	    case INIT_PORT_CWDIR:
	      if (fd == AT_FDCWD || file[0] == '/')
		return (reauthenticate (INIT_PORT_CWDIR, &rcwdir) ?:
			(*operate) (rcwdir));
	      else
		return (reauthenticate_cwdir_at (&rcwdir) ?:
			(*operate) (rcwdir));
	    default:
	      return _hurd_ports_use (which, operate);
	    }
	}

      rcrdir = rcwdir = MACH_PORT_NULL;

      HURD_CRITICAL_BEGIN;

      __mutex_lock (&_hurd_id.lock);
      /* Get _hurd_id up to date.  */
      if (err = _hurd_check_ids ())
	goto lose;

      if (_hurd_id.rid_auth == MACH_PORT_NULL)
	{
	  /* Set up _hurd_id.rid_auth.  This is a special auth server port
	     which uses the real uid and gid (the first aux uid and gid) as
	     the only effective uid and gid.  */

	  if (_hurd_id.aux.nuids < 1 || _hurd_id.aux.ngids < 1)
	    {
	      /* We do not have a real UID and GID.  Lose, lose, lose!  */
	      err = EGRATUITOUS;
	      goto lose;
	    }

	  /* Create a new auth port using our real UID and GID (the first
	     auxiliary UID and GID) as the only effective IDs.  */
	  if (err = __USEPORT (AUTH,
			       __auth_makeauth (port,
						NULL, MACH_MSG_TYPE_COPY_SEND, 0,
						_hurd_id.aux.uids, 1,
						_hurd_id.aux.uids,
						_hurd_id.aux.nuids,
						_hurd_id.aux.gids, 1,
						_hurd_id.aux.gids,
						_hurd_id.aux.ngids,
						&_hurd_id.rid_auth)))
	    goto lose;
	}

      if (!err)
	/* Look up the file name using the modified init ports.  */
	err = __hurd_file_name_lookup (&init_port, &__getdport, 0,
				       file, hurd_flags, 0, &io);

      /* We are done with _hurd_id.rid_auth now.  */
     lose:
      __mutex_unlock (&_hurd_id.lock);

      HURD_CRITICAL_END;

      if (rcrdir != MACH_PORT_NULL)
	__mach_port_deallocate (__mach_task_self (), rcrdir);
      if (rcwdir != MACH_PORT_NULL)
	__mach_port_deallocate (__mach_task_self (), rcwdir);
      if (err)
	return errfunc (err);
    }

  /* Find out what types of access we are allowed to this file.  */
  err = __file_check_access (io, &allowed);
  __mach_port_deallocate (__mach_task_self (), io);
  if (err)
    return errfunc (err);

  flags = 0;
  if (type & R_OK)
    flags |= O_READ;
  if (type & W_OK)
    flags |= O_WRITE;
  if (type & X_OK)
    flags |= O_EXEC;

  if (flags & ~allowed)
    /* We are not allowed all the requested types of access.  */
    return errfunc (EACCES);

  return 0;
}

int
__faccessat_noerrno (int fd, const char *file, int type, int at_flags)
{
  return __faccessat_common (fd, file, type, at_flags, hurd_fail_noerrno);
}

int
__faccessat (int fd, const char *file, int type, int at_flags)
{
  return __faccessat_common (fd, file, type, at_flags, hurd_fail_seterrno);
}
weak_alias (__faccessat, faccessat)