1 /* hairy bits of Hurd file name lookup
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
20 #include <hurd/lookup.h>
21 #include <hurd/term.h>
22 #include <hurd/paths.h>
27 #include <eloop-threshold.h>
29 /* Translate the error from dir_lookup into the error the user sees. */
31 lookup_error (error_t error
)
37 /* These indicate that the server does not understand dir_lookup
38 at all. If it were a directory, it would, by definition. */
46 __hurd_file_name_lookup_retry (error_t (*use_init_port
)
47 (int which
, error_t (*operate
) (file_t
)),
48 file_t (*get_dtable_port
) (int fd
),
50 (file_t dir
, const char *name
,
51 int flags
, mode_t mode
,
52 retry_type
*do_retry
, string_t retry_name
,
54 enum retry_type doretry
,
56 int flags
, mode_t mode
,
63 error_t
lookup_op (file_t startdir
)
65 if (file_name
[0] == '/' && file_name
[1] != '\0')
67 while (file_name
[1] == '/')
68 /* Remove double leading slash. */
70 if (file_name
[1] != '\0')
71 /* Remove leading slash when we have more than the slash. */
75 return lookup_error ((*lookup
) (startdir
, file_name
, flags
, mode
,
76 &doretry
, retryname
, result
));
78 error_t
reauthenticate (file_t unauth
)
81 mach_port_t ref
= __mach_reply_port ();
82 error_t
reauth (auth_t auth
)
84 return __auth_user_authenticate (auth
, ref
,
85 MACH_MSG_TYPE_MAKE_SEND
,
88 err
= __io_reauthenticate (unauth
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
90 err
= (*use_init_port
) (INIT_PORT_AUTH
, &reauth
);
91 __mach_port_destroy (__mach_task_self (), ref
);
92 __mach_port_deallocate (__mach_task_self (), unauth
);
97 lookup
= __dir_lookup
;
103 file_t startdir
= MACH_PORT_NULL
;
104 int dirport
= INIT_PORT_CWDIR
;
108 case FS_RETRY_REAUTH
:
109 if (err
= reauthenticate (*result
))
113 case FS_RETRY_NORMAL
:
114 if (nloops
++ >= __eloop_threshold ())
116 __mach_port_deallocate (__mach_task_self (), *result
);
120 /* An empty RETRYNAME indicates we have the final port. */
121 if (retryname
[0] == '\0' &&
122 /* If reauth'd, we must do one more retry on "" to give the new
123 translator a chance to make a new port for us. */
124 doretry
== FS_RETRY_NORMAL
)
126 if (flags
& O_NOFOLLOW
)
128 /* In Linux, O_NOFOLLOW means to reject symlinks. If we
129 did an O_NOLINK lookup above and io_stat here to check
130 for S_IFLNK, a translator like firmlink could easily
131 spoof this check by not showing S_IFLNK, but in fact
132 redirecting the lookup to some other name
133 (i.e. opening the very same holes a symlink would).
135 Instead we do an O_NOTRANS lookup above, and stat the
136 underlying node: if it has a translator set, and its
137 owner is not root (st_uid 0) then we reject it.
138 Since the motivation for this feature is security, and
139 that security presumes we trust the containing
140 directory, this check approximates the security of
141 refusing symlinks while accepting mount points.
142 Note that we actually permit something Linux doesn't:
143 we follow root-owned symlinks; if that is deemed
144 undesireable, we can add a final check for that
145 one exception to our general translator-based rule. */
147 err
= __io_stat (*result
, &st
);
149 && (st
.st_mode
& (S_IPTRANS
|S_IATRANS
)))
153 else if (st
.st_mode
& S_IPTRANS
)
157 size_t translen
= sizeof buf
;
158 err
= __file_get_translator (*result
,
161 && translen
> sizeof _HURD_SYMLINK
163 _HURD_SYMLINK
, sizeof _HURD_SYMLINK
))
169 /* We got a successful translation. Now apply any open-time
170 action flags we were passed. */
172 if (!err
&& (flags
& O_TRUNC
)) /* Asked to truncate the file. */
173 err
= __file_set_size (*result
, 0);
176 __mach_port_deallocate (__mach_task_self (), *result
);
181 file_name
= retryname
;
184 case FS_RETRY_MAGICAL
:
185 switch (retryname
[0])
188 dirport
= INIT_PORT_CRDIR
;
189 if (*result
!= MACH_PORT_NULL
)
190 __mach_port_deallocate (__mach_task_self (), *result
);
191 if (nloops
++ >= __eloop_threshold ())
193 file_name
= &retryname
[1];
197 if (retryname
[1] == 'd' && retryname
[2] == '/')
203 fd
= (int) __strtoul_internal (&retryname
[3], &end
, 10, 0);
204 if (end
== NULL
|| errno
|| /* Malformed number. */
205 /* Check for excess text after the number. A slash
206 is valid; it ends the component. Anything else
207 does not name a numeric file descriptor. */
208 (*end
!= '/' && *end
!= '\0'))
213 if (! get_dtable_port
)
217 *result
= (*get_dtable_port
) (fd
);
218 if (*result
== MACH_PORT_NULL
)
220 /* If the name was a proper number, but the file
221 descriptor does not exist, we return EBADF instead
234 /* Do a normal retry on the remaining components. */
236 file_name
= end
+ 1; /* Skip the slash. */
245 if (retryname
[1] == 'a' && retryname
[2] == 'c' &&
246 retryname
[3] == 'h' && retryname
[4] == 't' &&
247 retryname
[5] == 'y' && retryname
[6] == 'p' &&
251 struct host_basic_info hostinfo
;
252 mach_msg_type_number_t hostinfocnt
= HOST_BASIC_INFO_COUNT
;
254 /* XXX want client's host */
255 if (err
= __host_info (__mach_host_self (), HOST_BASIC_INFO
,
256 (integer_t
*) &hostinfo
,
259 if (hostinfocnt
!= HOST_BASIC_INFO_COUNT
)
261 p
= _itoa (hostinfo
.cpu_subtype
, &retryname
[8], 10, 0);
263 p
= _itoa (hostinfo
.cpu_type
, &retryname
[8], 10, 0);
265 abort (); /* XXX write this right if this ever happens */
267 strcpy (retryname
, p
);
275 if (retryname
[1] == 't' && retryname
[2] == 'y')
276 switch (retryname
[3])
278 error_t
opentty (file_t
*result
)
281 error_t
ctty_open (file_t port
)
283 if (port
== MACH_PORT_NULL
)
284 return ENXIO
; /* No controlling terminal. */
285 return __termctty_open_terminal (port
,
289 err
= (*use_init_port
) (INIT_PORT_CTTYID
, &ctty_open
);
291 err
= reauthenticate (*result
);
296 return opentty (result
);
298 if (err
= opentty (&startdir
))
300 strcpy (retryname
, &retryname
[4]);
319 if (startdir
!= MACH_PORT_NULL
)
321 err
= lookup_op (startdir
);
322 __mach_port_deallocate (__mach_task_self (), startdir
);
323 startdir
= MACH_PORT_NULL
;
326 err
= (*use_init_port
) (dirport
, &lookup_op
);
331 weak_alias (__hurd_file_name_lookup_retry
, hurd_file_name_lookup_retry
)