Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ntea.cc « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2fb205a047480eacfaa1db4a168f9c5375979adc (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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/* ntea.cc: code for manipulating Extended Attributes

   Copyright 1997, 1998, 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009,
   2010, 2011, 2014 Red Hat, Inc.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include "winsup.h"
#include "cygtls.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "ntdll.h"
#include "tls_pbuf.h"
#include <stdlib.h>
#include <attr/xattr.h>

#define MAX_EA_NAME_LEN    256
#define MAX_EA_VALUE_LEN 65536

/* At least one maximum sized entry fits. 
   CV 2014-04-04: NtQueryEaFile function chokes on buffers bigger than 64K
		  with STATUS_INVALID_PARAMETER if the handle points to a file
		  on a remote share, at least on Windows 7 and later.
		  In theory the buffer should have a size of
		  
		    sizeof (FILE_FULL_EA_INFORMATION) + MAX_EA_NAME_LEN
		    + MAX_EA_VALUE_LEN
		  
		  (65804 bytes), but we're opting for simplicity here, and
		  a 64K buffer has the advantage that we can use a tmp_pathbuf
		  buffer, rather than having to alloca 64K from stack. */
#define EA_BUFSIZ MAX_EA_VALUE_LEN

#define NEXT_FEA(p) ((PFILE_FULL_EA_INFORMATION) (p->NextEntryOffset \
		     ? (char *) p + p->NextEntryOffset : NULL))

ssize_t __reg3
read_ea (HANDLE hdl, path_conv &pc, const char *name, char *value, size_t size)
{
  OBJECT_ATTRIBUTES attr;
  NTSTATUS status;
  IO_STATUS_BLOCK io;
  ssize_t ret = -1;
  HANDLE h = hdl;
  ULONG glen = 0;
  PFILE_GET_EA_INFORMATION gea = NULL;
  PFILE_FULL_EA_INFORMATION fea;
  tmp_pathbuf tp;
  /* We have to store the latest EaName to compare with the next one, since
     NtQueryEaFile has a bug when accessing files on a remote share.  It
     returns the last EA entry of the file infinitely.  Even utilizing the
     optional EaIndex only helps marginally.  If you use that, the last
     EA in the file is returned twice. */
  char lastname[MAX_EA_NAME_LEN];

  myfault efault;
  if (efault.faulted (EFAULT))
    goto out;

  pc.get_object_attr (attr, sec_none_nih);

  debug_printf ("read_ea (%S, %s, %p, %lu)",
		attr.ObjectName, name, value, size);

  /* Early open if handle is NULL.  This allows to return error codes like
     ENOENT before we actually check for the correctness of the EA name and
     stuff like that. */
  if (!hdl)
    {
      status = NtOpenFile (&h, READ_CONTROL | FILE_READ_EA, &attr, &io,
			   FILE_SHARE_VALID_FLAGS, FILE_OPEN_FOR_BACKUP_INTENT);
      if (!NT_SUCCESS (status))
	{
	  __seterrno_from_nt_status (status);
	  goto out;
	}
      hdl = NULL;
    }

  fea = (PFILE_FULL_EA_INFORMATION) tp.w_get ();

  if (name)
    {
      size_t nlen;

      /* For compatibility with Linux, we only allow user xattrs and
	 return ENOTSUP otherwise. */
      if (ascii_strncasematch (name, "user.", 5))
	name += 5;
      else
	{
	  set_errno (ENOTSUP);
	  goto out;
	}

      if ((nlen = strlen (name)) >= MAX_EA_NAME_LEN)
	{
	  set_errno (EINVAL);
	  return -1;
	}
      glen = sizeof (FILE_GET_EA_INFORMATION) + nlen;
      gea = (PFILE_GET_EA_INFORMATION) alloca (glen);

      gea->NextEntryOffset = 0;
      gea->EaNameLength = nlen;
      strcpy (gea->EaName, name);
    }

  while (true)
    {
      if (h)
	{
	  status = NtQueryEaFile (h, &io, fea, EA_BUFSIZ, TRUE, gea, glen,
				  NULL, TRUE);
	  if (status != STATUS_ACCESS_DENIED || !hdl)
	    break;
	}
      status = NtOpenFile (&h, READ_CONTROL | FILE_READ_EA, &attr, &io,
			   FILE_SHARE_VALID_FLAGS, FILE_OPEN_FOR_BACKUP_INTENT);
      if (!NT_SUCCESS (status))
	break;
      hdl = NULL;
    }
  if (!NT_SUCCESS (status))
    {
      switch (status)
	{
	case STATUS_NO_EAS_ON_FILE:
	  ret = 0;
	  break;
	case STATUS_INVALID_DEVICE_REQUEST:
	  set_errno (ENOTSUP);
	  break;
	case STATUS_NOT_FOUND:
	  /* STATUS_NOT_FOUND is returned when calling NtQueryEaFile on NFS.
	     In theory this should mean that the file just has no EAs, but in
	     fact NFS doesn't support EAs, other than the EAs which are used
	     for NFS requests.  We're playing safe and convert STATUS_NOT_FOUND
	     to ENOATTR, unless we're on NFS, where we convert it to ENOTSUP. */
	  set_errno (pc.fs_is_nfs () ? ENOTSUP : ENOATTR);
	  break;
	case STATUS_NONEXISTENT_EA_ENTRY:
	  /* Actually STATUS_NONEXISTENT_EA_ENTRY is either never generated, or
	     it was only generated in some old and long forgotton NT version.
	     See below.  For safty reasons, we handle it here, nevertheless. */
	  set_errno (ENOATTR);
	  break;
	default:
	  __seterrno_from_nt_status (status);
	  break;
	}
      goto out;
    }
  if (name)
    {
      /* Another weird behaviour of NtQueryEaFile.  If you ask for a
	 specific EA which is not present in the file's EA list, you don't
	 get a useful error code like STATUS_NONEXISTENT_EA_ENTRY.  Rather
	 NtQueryEaFile returns success with the entry's EaValueLength
	 set to 0. */
      if (!fea->EaValueLength)
	{
	  set_errno (ENOATTR);
	  goto out;
	}
      if (size > 0)
	{
	  if (size < fea->EaValueLength)
	    {
	      set_errno (ERANGE);
	      goto out;
	    }
	  memcpy (value, fea->EaName + fea->EaNameLength + 1,
		  fea->EaValueLength);
	}
      ret = fea->EaValueLength;
    }
  else
    {
      ret = 0;
      do
	{
	  fea->EaNameLength += 5;	/* "user." */
	  if (size > 0)
	    {
	      if ((size_t) ret + fea->EaNameLength + 1 > size)
		{
		  set_errno (ERANGE);
		  goto out;
		}
	      /* For compatibility with Linux, we always prepend "user." to
		 the attribute name, so effectively we only support user
		 attributes from a application point of view. */
	      char tmpbuf[MAX_EA_NAME_LEN * 2];
	      char *tp = stpcpy (tmpbuf, "user.");
	      stpcpy (tp, fea->EaName);
	      /* NTFS stores all EA names in uppercase unfortunately.  To keep
		 compatibility with ext/xfs EA namespaces and accompanying
		 tools, which expect the namespaces to be lower case, we return
		 EA names in lowercase if the file is on a native NTFS. */
	      if (pc.fs_is_ntfs ())
		strlwr (tp);
	      tp = stpcpy (value, tmpbuf) + 1;
	      ret += tp - value;
	      value = tp;
	    }
	  else
	    ret += fea->EaNameLength + 1;
	  strcpy (lastname, fea->EaName);
	  status = NtQueryEaFile (h, &io, fea, EA_BUFSIZ, TRUE, NULL, 0,
				  NULL, FALSE);
	}
      while (NT_SUCCESS (status) && strcmp (lastname, fea->EaName) != 0);
    }

out:
  if (!hdl)
    CloseHandle (h);
  debug_printf ("%d = read_ea(%S, %s, %p, %lu)",
		ret, attr.ObjectName, name, value, size);
  return ret;
}

int __reg3
write_ea (HANDLE hdl, path_conv &pc, const char *name, const char *value,
	  size_t size, int flags)
{
  OBJECT_ATTRIBUTES attr;
  NTSTATUS status;
  IO_STATUS_BLOCK io;
  int ret = -1;
  HANDLE h = hdl;
  PFILE_FULL_EA_INFORMATION fea;
  ULONG flen;
  size_t nlen;

  myfault efault;
  if (efault.faulted (EFAULT))
    goto out;

  pc.get_object_attr (attr, sec_none_nih);

  debug_printf ("write_ea (%S, %s, %p, %lu, %d)",
		attr.ObjectName, name, value, size, flags);

  /* Early open if handle is NULL.  This allows to return error codes like
     ENOENT before we actually check for the correctness of the EA name and
     stuff like that. */
  if (!hdl)
    {
      status = NtOpenFile (&h, READ_CONTROL | FILE_WRITE_EA, &attr, &io,
			   FILE_SHARE_VALID_FLAGS, FILE_OPEN_FOR_BACKUP_INTENT);
      if (!NT_SUCCESS (status))
	{
	  __seterrno_from_nt_status (status);
	  goto out;
	}
      hdl = NULL;
    }

  /* For compatibility with Linux, we only allow user xattrs and
     return ENOTSUP otherwise. */
  if (!ascii_strncasematch (name, "user.", 5))
    {
      set_errno (ENOTSUP);
      goto out;
    }

  /* removexattr is supposed to fail with ENOATTR if the requested EA is not
     available.  This is equivalent to the XATTR_REPLACE flag for setxattr. */
  if (!value)
    flags = XATTR_REPLACE;

  if (flags)
    {
      if (flags != XATTR_CREATE && flags != XATTR_REPLACE)
	{
	  set_errno (EINVAL);
	  goto out;
	}
      ssize_t rret = read_ea (hdl, pc, name, NULL, 0);
      if (flags == XATTR_CREATE && rret > 0)
	{
	  set_errno (EEXIST);
	  goto out;
	}
      if (flags == XATTR_REPLACE && rret < 0)
	goto out;
    }

  /* Skip "user." prefix. */
  name += 5;

  if ((nlen = strlen (name)) >= MAX_EA_NAME_LEN)
    {
      set_errno (EINVAL);
      goto out;
    }
  flen = sizeof (FILE_FULL_EA_INFORMATION) + nlen + 1 + size;
  fea = (PFILE_FULL_EA_INFORMATION) alloca (flen);
  fea->NextEntryOffset = 0;
  fea->Flags = 0;
  fea->EaNameLength = nlen;
  fea->EaValueLength = size;
  strcpy (fea->EaName, name);
  if (value)
    memcpy (fea->EaName + fea->EaNameLength + 1, value, size);

  while (true)
    {
      if (h)
	{
	  status = NtSetEaFile (h, &io, fea, flen);
	  if (status != STATUS_ACCESS_DENIED || !hdl)
	    break;
	}
      status = NtOpenFile (&h, READ_CONTROL | FILE_WRITE_EA, &attr, &io,
			   FILE_SHARE_VALID_FLAGS, FILE_OPEN_FOR_BACKUP_INTENT);
      if (!NT_SUCCESS (status))
	break;
      hdl = NULL;
    }
  if (!NT_SUCCESS (status))
    {
      switch (status)
	{
	case STATUS_EA_TOO_LARGE:
	  /* STATUS_EA_TOO_LARGE has a matching Win32 error ERROR_EA_TABLE_FULL.
	     For some unknown reason RtlNtStatusToDosError does not translate
	     STATUS_EA_TOO_LARGE to ERROR_EA_TABLE_FULL, but instead to
	     ERROR_EA_LIST_INCONSISTENT.  This error code is also returned for
	     STATUS_EA_LIST_INCONSISTENT, which means the incoming EA list is...
	     inconsistent.  For obvious reasons we translate
	     ERROR_EA_LIST_INCONSISTENT to EINVAL, so we have to handle
	     STATUS_EA_TOO_LARGE explicitely here, to get the correct mapping
	     to ENOSPC. */
	  set_errno (ENOSPC);
	  break;
	case STATUS_INVALID_DEVICE_REQUEST:
	  set_errno (ENOTSUP);
	  break;
	default:
	  __seterrno_from_nt_status (status);
	  break;
	}
    }
  else
    ret = 0;

out:
  if (!hdl)
    CloseHandle (h);
  debug_printf ("%d = write_ea(%S, %s, %p, %lu, %d)",
		ret, attr.ObjectName, name, value, size, flags);
  return ret;
}

static ssize_t __stdcall
getxattr_worker (path_conv &pc, const char *name, void *value, size_t size)
{
  int res = -1;

  if (pc.error)
    {
      debug_printf ("got %d error from path_conv", pc.error);
      set_errno (pc.error);
    }
  else if (pc.exists ())
    {
      fhandler_base *fh;

      if (!(fh = build_fh_pc (pc)))
	return -1;

      res = fh->fgetxattr (name, value, size);
      delete fh;
    }
  else
    set_errno (ENOENT);
  return res;
}

extern "C" ssize_t
getxattr (const char *path, const char *name, void *value, size_t size)
{
  if (!name)
    {
      set_errno (EINVAL);
      return -1;
    }
  path_conv pc (path, PC_SYM_FOLLOW | PC_POSIX, stat_suffixes);
  return getxattr_worker (pc, name, value, size);
}

extern "C" ssize_t
lgetxattr (const char *path, const char *name, void *value, size_t size)
{
  if (!name)
    {
      set_errno (EINVAL);
      return -1;
    }
  path_conv pc (path, PC_SYM_NOFOLLOW | PC_POSIX, stat_suffixes);
  return getxattr_worker (pc, name, value, size);
}

extern "C" ssize_t
fgetxattr (int fd, const char *name, void *value, size_t size)
{
  int res;

  if (!name)
    {
      set_errno (EINVAL);
      return -1;
    }
  cygheap_fdget cfd (fd);
  if (cfd < 0)
    res = -1;
  else
    res = cfd->fgetxattr (name, value, size);
  return res;
}

extern "C" ssize_t
listxattr (const char *path, char *list, size_t size)
{
  path_conv pc (path, PC_SYM_FOLLOW | PC_POSIX, stat_suffixes);
  return getxattr_worker (pc, NULL, list, size);
}

extern "C" ssize_t
llistxattr (const char *path, char *list, size_t size)
{
  path_conv pc (path, PC_SYM_NOFOLLOW | PC_POSIX, stat_suffixes);
  return getxattr_worker (pc, NULL, list, size);
}

extern "C" ssize_t
flistxattr (int fd, char *list, size_t size)
{
  int res;

  cygheap_fdget cfd (fd);
  if (cfd < 0)
    res = -1;
  else
    res = cfd->fgetxattr (NULL, list, size);
  return res;
}

static int __stdcall
setxattr_worker (path_conv &pc, const char *name, const void *value,
		 size_t size, int flags)
{
  int res = -1;

  if (pc.error)
    {
      debug_printf ("got %d error from path_conv", pc.error);
      set_errno (pc.error);
    }
  else if (pc.exists ())
    {
      fhandler_base *fh;

      if (!(fh = build_fh_pc (pc)))
	return -1;

      res = fh->fsetxattr (name, value, size, flags);
      delete fh;
    }
  else
    set_errno (ENOENT);
  return res;
}

extern "C" int
setxattr (const char *path, const char *name, const void *value, size_t size,
	  int flags)
{
  if (!size)
    {
      set_errno (EINVAL);
      return -1;
    }
  path_conv pc (path, PC_SYM_NOFOLLOW | PC_POSIX, stat_suffixes);
  return setxattr_worker (pc, name, value, size, flags);
}

extern "C" int
lsetxattr (const char *path, const char *name, const void *value, size_t size,
	   int flags)
{
  if (!size)
    {
      set_errno (EINVAL);
      return -1;
    }
  path_conv pc (path, PC_SYM_NOFOLLOW | PC_POSIX, stat_suffixes);
  return setxattr_worker (pc, name, value, size, flags);
}

extern "C" int
fsetxattr (int fd, const char *name, const void *value, size_t size, int flags)
{
  int res;

  if (!size)
    {
      set_errno (EINVAL);
      return -1;
    }
  cygheap_fdget cfd (fd);
  if (cfd < 0)
    res = -1;
  else
    res = cfd->fsetxattr (name, value, size, flags);
  return res;
}

extern "C" int
removexattr (const char *path, const char *name)
{
  path_conv pc (path, PC_SYM_FOLLOW | PC_POSIX, stat_suffixes);
  return setxattr_worker (pc, name, NULL, 0, 0);
}

extern "C" int
lremovexattr (const char *path, const char *name)
{
  path_conv pc (path, PC_SYM_NOFOLLOW | PC_POSIX, stat_suffixes);
  return setxattr_worker (pc, name, NULL, 0, 0);
}

extern "C" int
fremovexattr (int fd, const char *name)
{
  int res;

  cygheap_fdget cfd (fd);
  if (cfd < 0)
    res = -1;
  else
    res = cfd->fsetxattr (name, NULL, 0, 0);
  return res;
}