summaryrefslogtreecommitdiff
path: root/libnetfs/dir-lookup.c
blob: 731e53cb90397597bb9a8252774a7ebb194d7fb8 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
   Copyright (C) 1995,96,97,98,99,2000,01,02,13,14
     Free Software Foundation, Inc.
   Written by Michael I. Bushnell, p/BSG.

   This file is part of the GNU Hurd.

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

   The GNU Hurd 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <hurd/paths.h>
#include "netfs.h"
#include "fs_S.h"
#include "callbacks.h"
#include "misc.h"

error_t
netfs_S_dir_lookup (struct protid *dircred,
		    char *filename,
		    int flags,
		    mode_t mode,
		    retry_type *do_retry,
		    char *retry_name,
		    mach_port_t *retry_port,
		    mach_msg_type_name_t *retry_port_type)
{
  int create;			/* true if O_CREAT flag set */
  int excl;			/* true if O_EXCL flag set */
  int mustbedir = 0;		/* true if the result must be S_IFDIR */
  int lastcomp = 0;		/* true if we are at the last component */
  int newnode = 0;		/* true if this node is newly created */
  int nsymlinks = 0;
  struct node *dnp, *np;
  char *nextname;
  char *relpath;
  error_t err;
  struct protid *newpi = NULL;
  struct iouser *user;

  if (!dircred)
    return EOPNOTSUPP;

  create = (flags & O_CREAT);
  excl = (flags & O_EXCL);

  /* Skip leading slashes */
  while (*filename == '/')
    filename++;

  /* Preserve the path relative to dircred->po->path.  */
  relpath = strdup (filename);
  if (! relpath)
    return ENOMEM;

  /* Keep a pointer to the start of the filename for length
     calculations.  */
  char *filename_start = filename;

  *retry_port_type = MACH_MSG_TYPE_MAKE_SEND;
  *do_retry = FS_RETRY_NORMAL;
  *retry_name = '\0';

  if (*filename == '\0')
    {
      /* Set things up in the state expected by the code from gotit: on. */
      dnp = 0;
      np = dircred->po->np;
      pthread_mutex_lock (&np->lock);
      netfs_nref (np);
      goto gotit;
    }

  dnp = dircred->po->np;
  pthread_mutex_lock (&dnp->lock);

  netfs_nref (dnp);		/* acquire a reference for later netfs_nput */

  do
    {
      assert (!lastcomp);

      /* Find the name of the next pathname component */
      nextname = index (filename, '/');

      if (nextname)
	{
	  *nextname++ = '\0';
	  while (*nextname == '/')
	    nextname++;
	  if (*nextname == '\0')
	    {
	      /* These are the rules for filenames ending in /. */
	      nextname = 0;
	      lastcomp = 1;
	      mustbedir = 1;
	      create = 0;
	    }
	  else
	    lastcomp = 0;
	}
      else
	lastcomp = 1;

      np = 0;

    retry_lookup:

      if ((dnp == netfs_root_node || dnp == dircred->po->shadow_root)
	  && filename[0] == '.' && filename[1] == '.' && filename[2] == '\0')
	if (dnp == dircred->po->shadow_root)
	  /* We're at the root of a shadow tree.  */
	  {
	    if (dircred->po->shadow_root_parent == MACH_PORT_NULL)
	      {
		/* This is a shadow root with no parent, meaning
		   we should treat it as a virtual root disconnected
		   from its real .. directory.	*/
		err = 0;
		np = dnp;
		netfs_nref (np);
	      }
	    else
	      {
		/* Punt the client up to the shadow root parent.  */
		*do_retry = FS_RETRY_REAUTH;
		*retry_port = dircred->po->shadow_root_parent;
		*retry_port_type = MACH_MSG_TYPE_COPY_SEND;
		if (lastcomp && mustbedir) /* Trailing slash.  */
		  strcpy (retry_name, "/");
		else if (!lastcomp)
		  strcpy (retry_name, nextname);
		err = 0;
		pthread_mutex_unlock (&dnp->lock);
		goto out;
	      }
	  }
	else if (dircred->po->root_parent != MACH_PORT_NULL)
	  /* We're at a real translator root; even if DIRCRED->po has a
	     shadow root, we can get here if its in a directory that was
	     renamed out from under it...  */
	  {
	    *do_retry = FS_RETRY_REAUTH;
	    *retry_port = dircred->po->root_parent;
	    *retry_port_type = MACH_MSG_TYPE_COPY_SEND;
	    if (lastcomp && mustbedir) /* Trailing slash.  */
	      strcpy (retry_name, "/");
	    else if (!lastcomp)
	      strcpy (retry_name, nextname);
	    err = 0;
	    pthread_mutex_unlock (&dnp->lock);
	    goto out;
	  }
	else
	  /* We are global root */
	  {
	    err = 0;
	    np = dnp;
	    netfs_nref (np);
	  }
      else
	/* Attempt a lookup on the next pathname component. */
	err = netfs_attempt_lookup (dircred->user, dnp, filename, &np);

      /* At this point, DNP is unlocked */

      /* Implement O_EXCL flag here */
      if (lastcomp && create && excl && !err)
	err = EEXIST;

      /* Create the new node if necessary */
      if (lastcomp && create && err == ENOENT)
	{
	  mode &= ~(S_IFMT | S_ISPARE | S_ISVTX);
	  mode |= S_IFREG;
	  pthread_mutex_lock (&dnp->lock);
	  err = netfs_attempt_create_file (dircred->user, dnp,
					   filename, mode, &np);

	  /* If someone has already created the file (between our lookup
	     and this create) then we just got EEXIST.  If we are
	     EXCL, that's fine; otherwise, we have to retry the lookup. */
	  if (err == EEXIST && !excl)
	    {
	      pthread_mutex_lock (&dnp->lock);
	      goto retry_lookup;
	    }

	  newnode = 1;
	}

      /* All remaining errors get returned to the user */
      if (err)
	goto out;

      err = netfs_validate_stat (np, dircred->user);
      if (err)
	goto out;

      if ((((flags & O_NOTRANS) == 0) || !lastcomp || mustbedir)
	  && ((np->nn_translated & S_IPTRANS)
	      || S_ISFIFO (np->nn_translated)
	      || S_ISCHR (np->nn_translated)
	      || S_ISBLK (np->nn_translated)
	      || fshelp_translated (&np->transbox)))
	{
	  mach_port_t dirport;

	  /* Create an unauthenticated port for DNP, and then
	     unlock it. */
	  err = iohelp_create_empty_iouser (&user);
	  if (! err)
	    {
	      newpi = netfs_make_protid (netfs_make_peropen (dnp, 0,
							     dircred->po),
					 user);
	      if (! newpi)
	        {
		  err = errno;
		  iohelp_free_iouser (user);
		}
	    }

	  boolean_t register_translator = 0;
	  if (! err)
	    {
	      struct fshelp_stat_cookie2 cookie = {
		.statp = &np->nn_stat,
		.modep = &np->nn_translated,
		.next = dircred->po,
	      };

	      dirport = ports_get_send_right (newpi);

	      /* Check if an active translator is currently running.  If
		 not, fshelp_fetch_root will start one.  In that case, we
		 need to register it in the list of active
		 translators.  */
	      register_translator = np->transbox.active == MACH_PORT_NULL;

	      err = fshelp_fetch_root (&np->transbox,
				       &cookie,
				       dirport,
				       dircred->user,
				       lastcomp ? flags : 0,
				       ((np->nn_translated & S_IPTRANS)
					? _netfs_translator_callback1
					: fshelp_short_circuited_callback1),
				       _netfs_translator_callback2,
				       do_retry, retry_name, retry_port);
	      /* fetch_root copies DIRPORT for success, so we always should
		 deallocate our send right.  */
	      mach_port_deallocate (mach_task_self (), dirport);
	    }

	  if (err != ENOENT)
	    {
	      *retry_port_type = MACH_MSG_TYPE_MOVE_SEND;
	      if (!err)
		{
		  char *end = strchr (retry_name, '\0');
		  if (mustbedir)
		    *end++ = '/'; /* Trailing slash.  */
		  else if (!lastcomp) {
		    if (end != retry_name)
		      *end++ = '/';
		    strcpy (end, nextname);
		  }
		}

	      if (register_translator)
		{
		  char *translator_path = strdupa (relpath);
		  char *complete_path;
		  if (nextname != NULL)
		    {
		      /* This was not the last path component.
			 NEXTNAME points to the next component, locate
			 the end of the current component and use it
			 to trim TRANSLATOR_PATH.  */
		      char *end = nextname;
		      while (*end != 0)
			end--;
		      translator_path[end - filename_start] = '\0';
		    }

		  if (dircred->po->path == NULL || !strcmp (dircred->po->path,"."))
		      /* dircred is the root directory.  */
		      complete_path = translator_path;
		  else
		      asprintf (&complete_path, "%s/%s", dircred->po->path, translator_path);

		  err = fshelp_set_active_translator (&newpi->pi,
						      complete_path,
						      np->transbox.active);
		  if (complete_path != translator_path)
		    free(complete_path);
		  if (err)
		    {
		      ports_port_deref (newpi);
		      goto out;
		    }
		}

	      ports_port_deref (newpi);
	      goto out;
	    }

	  ports_port_deref (newpi);

	  /* ENOENT means there was a hiccup, and the translator vanished
	     while NP was unlocked inside fshelp_fetch_root; continue as normal. */
	  err = 0;
	}

      if (S_ISLNK (np->nn_translated)
	  && (!lastcomp
	      || mustbedir	/* "foo/" must see that foo points to a dir */
	      || !(flags & (O_NOLINK|O_NOTRANS))))
	{
	  size_t nextnamelen, newnamelen, linklen;
	  char *linkbuf;

	  /* Handle symlink interpretation */
	  if (nsymlinks++ > netfs_maxsymlinks)
	    {
	      err = ELOOP;
	      goto out;
	    }

	  linklen = np->nn_stat.st_size;

	  nextnamelen = nextname ? strlen (nextname) + 1 : 0;
	  newnamelen = nextnamelen + linklen + 1 + 1;
	  linkbuf = alloca (newnamelen);

	  err = netfs_attempt_readlink (dircred->user, np, linkbuf);
	  if (err)
	    goto out;

	  if (nextname)
	    {
	      linkbuf[linklen] = '/';
	      memcpy (linkbuf + linklen + 1, nextname,
		     nextnamelen - 1);
	    }
	  if (mustbedir)
	    {
	      linkbuf[nextnamelen + linklen] = '/';
	      linkbuf[nextnamelen + linklen + 1] = '\0';
	    }
	  else
	    linkbuf[nextnamelen + linklen] = '\0';

	  if (linkbuf[0] == '/')
	    {
	      /* Punt to the caller */
	      *do_retry = FS_RETRY_MAGICAL;
	      *retry_port = MACH_PORT_NULL;
	      strcpy (retry_name, linkbuf);
	      goto out;
	    }

	  filename = linkbuf;
	  mustbedir = 0;
	  if (lastcomp)
	    {
	      lastcomp = 0;

	      /* Symlinks to nonexistent files aren't allowed to cause
		 creation, so clear the flag here. */
	      create = 0;
	    }
	  netfs_nput (np);
	  pthread_mutex_lock (&dnp->lock);
	  np = 0;
	}
      else
	{
	  /* Normal nodes here for next filename component */
	  filename = nextname;
	  netfs_nrele (dnp);

	  if (lastcomp)
	    dnp = 0;
	  else
	    {
	      dnp = np;
	      np = 0;
	    }
	}
    }
  while (filename && *filename);

  /* At this point, NP is the node to return.  */
 gotit:

  if (mustbedir)
    {
      netfs_validate_stat (np, dircred->user);
      if (!S_ISDIR (np->nn_stat.st_mode))
	{
	  err = ENOTDIR;
	  goto out;
	}
    }
  err = netfs_check_open_permissions (dircred->user, np,
				      flags, newnode);
  if (err)
    goto out;

  flags &= ~OPENONLY_STATE_MODES;

  err = iohelp_dup_iouser (&user, dircred->user);
  if (err)
    goto out;

  newpi = netfs_make_protid (netfs_make_peropen (np, flags, dircred->po),
			     user);
  if (! newpi)
    {
      iohelp_free_iouser (user);
      err = errno;
      goto out;
    }

  free (newpi->po->path);
  if (dircred->po->path == NULL || !strcmp (dircred->po->path,"."))
    {
      /* dircred is the root directory.  */
      newpi->po->path = relpath;
      relpath = NULL; /* Do not free relpath.  */
    }
  else
    {
      newpi->po->path = NULL;
      asprintf (&newpi->po->path, "%s/%s", dircred->po->path, relpath);
    }

  if (! newpi->po->path)
    err = errno;

  *retry_port = ports_get_right (newpi);
  ports_port_deref (newpi);

 out:
  if (np)
    netfs_nput (np);
  if (dnp)
    netfs_nrele (dnp);
  free (relpath);
  return err;
}