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
|
/* ntea.cc: code for manipulating NTEA information
Copyright 1997, 1998, 2000, 2001 Red Hat, Inc.
Written by Sergey S. Okhapkin (sos@prospect.com.ru)
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 <stdio.h>
#include <stdlib.h>
#include "security.h"
/* Default to not using NTEA information */
bool allow_ntea;
/*
From Windows NT DDK:
FILE_FULL_EA_INFORMATION provides extended attribute information.
This structure is used primarily by network drivers.
Members
NextEntryOffset
The offset of the next FILE_FULL_EA_INFORMATION-type entry. This member is
zero if no other entries follow this one.
Flags
Can be zero or can be set with FILE_NEED_EA, indicating that the file to which
the EA belongs cannot be interpreted without understanding the associated
extended attributes.
EaNameLength
The length in bytes of the EaName array. This value does not include a
zero-terminator to EaName.
EaValueLength
The length in bytes of each EA value in the array.
EaName
An array of characters naming the EA for this entry.
Comments
This structure is longword-aligned. If a set of FILE_FULL_EA_INFORMATION
entries is buffered, NextEntryOffset value in each entry, except the last,
falls on a longword boundary.
The value(s) associated with each entry follows the EaName array. That is, an
EA's values are located at EaName + (EaNameLength + 1).
*/
typedef struct _FILE_FULL_EA_INFORMATION {
ULONG NextEntryOffset;
UCHAR Flags;
UCHAR EaNameLength;
USHORT EaValueLength;
CHAR EaName[1];
} FILE_FULL_EA_INFORMATION, *PFILE_FULL_EA_INFORMATION;
/* Functions prototypes */
int NTReadEA (const char *file, const char *attrname, char *buf, int len);
static PFILE_FULL_EA_INFORMATION NTReadEARaw (HANDLE file, int *len);
BOOL NTWriteEA(const char *file, const char *attrname, char *buf, int len);
/*
* NTReadEA - read file's Extended Attribute.
*
* Parameters:
* file - pointer to filename
* attrname- pointer to EA name (case insensitivy. EAs are sored in upper
* case).
* attrbuf - pointer to buffer to store EA's value.
* len - length of attrbuf.
* Return value:
* 0 - if file or attribute "attrname" not found.
* N - number of bytes stored in attrbuf if succes.
* -1 - attrbuf too small for EA value.
*/
int __stdcall
NTReadEA (const char *file, const char *attrname, char *attrbuf, int len)
{
HANDLE hFileSource;
int eafound = 0;
PFILE_FULL_EA_INFORMATION ea, sea;
int easize;
hFileSource = CreateFile (file, FILE_READ_EA,
FILE_SHARE_READ | FILE_SHARE_WRITE,
&sec_none_nih, // sa
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (hFileSource == INVALID_HANDLE_VALUE)
return 0;
/* Read in raw array of EAs */
ea = sea = NTReadEARaw (hFileSource, &easize);
/* Search for requested attribute */
while (sea)
{
if (strcasematch (ea->EaName, attrname)) /* EA found */
{
if (ea->EaValueLength > len)
{
eafound = -1; /* buffer too small */
break;
}
memcpy (attrbuf, ea->EaName + (ea->EaNameLength + 1),
ea->EaValueLength);
eafound = ea->EaValueLength;
break;
}
if ((ea->NextEntryOffset == 0) || ((int) ea->NextEntryOffset > easize))
break;
ea = (PFILE_FULL_EA_INFORMATION) ((char *) ea + ea->NextEntryOffset);
}
if (sea)
free (sea);
CloseHandle (hFileSource);
return eafound;
}
/*
* NTReadEARaw - internal routine to read EAs array to malloced buffer. The
* caller should free this buffer after usage.
* Parameters:
* hFileSource - handle to file. This handle should have FILE_READ_EA
* rights.
* len - pointer to int variable where length of buffer will
* be stored.
* Return value:
* pointer to buffer with file's EAs, or NULL if any error occured.
*/
static PFILE_FULL_EA_INFORMATION
NTReadEARaw (HANDLE hFileSource, int *len)
{
WIN32_STREAM_ID StreamId;
DWORD dwBytesWritten;
LPVOID lpContext;
DWORD StreamSize;
PFILE_FULL_EA_INFORMATION eafound = NULL;
lpContext = NULL;
StreamSize = sizeof (WIN32_STREAM_ID) - sizeof (WCHAR**);
/* Read the WIN32_STREAM_ID in */
while (BackupRead (hFileSource, (LPBYTE) &StreamId, StreamSize,
&dwBytesWritten,
FALSE, // don't abort yet
FALSE, // don't process security
&lpContext))
{
DWORD sl,sh;
if (dwBytesWritten == 0) /* No more Stream IDs */
break;
/* skip StreamName */
if (StreamId.dwStreamNameSize)
{
unsigned char *buf;
buf = (unsigned char *) malloc (StreamId.dwStreamNameSize);
if (buf == NULL)
break;
if (!BackupRead (hFileSource, buf, // buffer to read
StreamId.dwStreamNameSize, // num bytes to read
&dwBytesWritten,
FALSE, // don't abort yet
FALSE, // don't process security
&lpContext)) // Stream name read error
{
free (buf);
break;
}
free (buf);
}
/* Is it EA stream? */
if (StreamId.dwStreamId == BACKUP_EA_DATA)
{
unsigned char *buf;
buf = (unsigned char *) malloc (StreamId.Size.LowPart);
if (buf == NULL)
break;
if (!BackupRead (hFileSource, buf, // buffer to read
StreamId.Size.LowPart, // num bytes to write
&dwBytesWritten,
FALSE, // don't abort yet
FALSE, // don't process security
&lpContext))
{
free (buf); /* EA read error */
break;
}
eafound = (PFILE_FULL_EA_INFORMATION) buf;
*len = StreamId.Size.LowPart;
break;
}
/* Skip current stream */
if (!BackupSeek (hFileSource,
StreamId.Size.LowPart,
StreamId.Size.HighPart,
&sl,
&sh,
&lpContext))
break;
}
/* free context */
BackupRead (
hFileSource,
NULL, // buffer to write
0, // number of bytes to write
&dwBytesWritten,
TRUE, // abort
FALSE, // don't process security
&lpContext);
return eafound;
}
/*
* NTWriteEA - write file's Extended Attribute.
*
* Parameters:
* file - pointer to filename
* attrname- pointer to EA name (case insensitivy. EAs are sored in upper
* case).
* buf - pointer to buffer with EA value.
* len - length of buf.
* Return value:
* true if success, false otherwice.
* Note: if len=0 given EA will be deleted.
*/
BOOL __stdcall
NTWriteEA (const char *file, const char *attrname, const char *buf, int len)
{
HANDLE hFileSource;
WIN32_STREAM_ID StreamId;
DWORD dwBytesWritten;
LPVOID lpContext;
DWORD StreamSize, easize;
bool bSuccess = false;
PFILE_FULL_EA_INFORMATION ea;
hFileSource = CreateFile (file, FILE_WRITE_EA,
FILE_SHARE_READ | FILE_SHARE_WRITE,
&sec_none_nih, // sa
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (hFileSource == INVALID_HANDLE_VALUE)
return FALSE;
lpContext = NULL;
StreamSize = sizeof (WIN32_STREAM_ID) - sizeof (WCHAR**);
/* FILE_FULL_EA_INFORMATION structure is longword-aligned */
easize = sizeof (*ea) - sizeof (WCHAR**) + strlen (attrname) + 1 + len
+ (sizeof (DWORD) - 1);
easize &= ~(sizeof (DWORD) - 1);
if ((ea = (PFILE_FULL_EA_INFORMATION) malloc (easize)) == NULL)
goto cleanup;
memset (ea, 0, easize);
ea->EaNameLength = strlen (attrname);
ea->EaValueLength = len;
strcpy (ea->EaName, attrname);
memcpy (ea->EaName + (ea->EaNameLength + 1), buf, len);
StreamId.dwStreamId = BACKUP_EA_DATA;
StreamId.dwStreamAttributes = 0;
StreamId.Size.HighPart = 0;
StreamId.Size.LowPart = easize;
StreamId.dwStreamNameSize = 0;
if (!BackupWrite (hFileSource, (LPBYTE) &StreamId, StreamSize,
&dwBytesWritten,
FALSE, // don't abort yet
FALSE, // don't process security
&lpContext))
goto cleanup;
if (!BackupWrite (hFileSource, (LPBYTE) ea, easize,
&dwBytesWritten,
FALSE, // don't abort yet
FALSE, // don't process security
&lpContext))
goto cleanup;
bSuccess = true;
/* free context */
cleanup:
BackupRead (hFileSource,
NULL, // buffer to write
0, // number of bytes to write
&dwBytesWritten,
TRUE, // abort
FALSE, // don't process security
&lpContext);
CloseHandle (hFileSource);
if (ea)
free (ea);
return bSuccess;
}
|