summaryrefslogtreecommitdiff
path: root/source4/libcli/raw/raweas.c
blob: 5f06e7001d8517fea8dc6d248e36fb7149564e46 (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
/* 
   Unix SMB/CIFS implementation.
   parsing of EA (extended attribute) lists
   Copyright (C) Andrew Tridgell 2003
   
   This program 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 3 of the License, or
   (at your option) any later version.
   
   This program 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, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "libcli/raw/libcliraw.h"
#include "libcli/raw/raw_proto.h"

/*
  work out how many bytes on the wire a ea list will consume. 
  This assumes the names are strict ascii, which should be a
  reasonable assumption
*/
size_t ea_list_size(unsigned int num_eas, struct ea_struct *eas)
{
	unsigned int total = 4;
	int i;
	for (i=0;i<num_eas;i++) {
		total += 4 + strlen(eas[i].name.s)+1 + eas[i].value.length;
	}
	return total;
}

/*
  work out how many bytes on the wire a ea name list will consume. 
*/
static unsigned int ea_name_list_size(unsigned int num_names, struct ea_name *eas)
{
	unsigned int total = 4;
	int i;
	for (i=0;i<num_names;i++) {
		total += 1 + strlen(eas[i].name.s) + 1;
	}
	return total;
}

/*
  work out how many bytes on the wire a chained ea list will consume.
  This assumes the names are strict ascii, which should be a
  reasonable assumption
*/
size_t ea_list_size_chained(unsigned int num_eas, struct ea_struct *eas, unsigned alignment)
{
	unsigned int total = 0;
	int i;
	for (i=0;i<num_eas;i++) {
		unsigned int len = 8 + strlen(eas[i].name.s)+1 + eas[i].value.length;
		len = (len + (alignment-1)) & ~(alignment-1);
		total += len;
	}
	return total;
}

/*
  put a ea_list into a pre-allocated buffer - buffer must be at least
  of size ea_list_size()
*/
void ea_put_list(uint8_t *data, unsigned int num_eas, struct ea_struct *eas)
{
	int i;
	uint32_t ea_size;

	ea_size = ea_list_size(num_eas, eas);

	SIVAL(data, 0, ea_size);
	data += 4;

	for (i=0;i<num_eas;i++) {
		unsigned int nlen = strlen(eas[i].name.s);
		SCVAL(data, 0, eas[i].flags);
		SCVAL(data, 1, nlen);
		SSVAL(data, 2, eas[i].value.length);
		memcpy(data+4, eas[i].name.s, nlen+1);
		memcpy(data+4+nlen+1, eas[i].value.data, eas[i].value.length);
		data += 4+nlen+1+eas[i].value.length;
	}
}


/*
  put a chained ea_list into a pre-allocated buffer - buffer must be
  at least of size ea_list_size()
*/
void ea_put_list_chained(uint8_t *data, unsigned int num_eas, struct ea_struct *eas,
			 unsigned alignment)
{
	int i;

	for (i=0;i<num_eas;i++) {
		unsigned int nlen = strlen(eas[i].name.s);
		uint32_t len = 8+nlen+1+eas[i].value.length;
		unsigned int pad = ((len + (alignment-1)) & ~(alignment-1)) - len;
		if (i == num_eas-1) {
			SIVAL(data, 0, 0);
		} else {
			SIVAL(data, 0, len+pad);
		}
		SCVAL(data, 4, eas[i].flags);
		SCVAL(data, 5, nlen);
		SSVAL(data, 6, eas[i].value.length);
		memcpy(data+8, eas[i].name.s, nlen+1);
		memcpy(data+8+nlen+1, eas[i].value.data, eas[i].value.length);
		memset(data+len, 0, pad);
		data += len + pad;
	}
}


/*
  pull a ea_struct from a buffer. Return the number of bytes consumed
*/
unsigned int ea_pull_struct(const DATA_BLOB *blob,
		      TALLOC_CTX *mem_ctx,
		      struct ea_struct *ea)
{
	uint8_t nlen;
	uint16_t vlen;

	ZERO_STRUCTP(ea);

	if (blob->length < 6) {
		return 0;
	}

	ea->flags = CVAL(blob->data, 0);
	nlen = CVAL(blob->data, 1);
	vlen = SVAL(blob->data, 2);

	if (nlen+1+vlen > blob->length-4) {
		return 0;
	}

	ea->name.s = talloc_strndup(mem_ctx, (const char *)(blob->data+4), nlen);
	ea->name.private_length = nlen;
	ea->value = data_blob_talloc(mem_ctx, NULL, vlen+1);
	if (!ea->value.data) return 0;
	if (vlen) {
		memcpy(ea->value.data, blob->data+4+nlen+1, vlen);
	}
	ea->value.data[vlen] = 0;
	ea->value.length--;

	return 4 + nlen+1 + vlen;
}


/*
  pull a ea_list from a buffer
*/
NTSTATUS ea_pull_list(const DATA_BLOB *blob, 
		      TALLOC_CTX *mem_ctx,
		      unsigned int *num_eas, struct ea_struct **eas)
{
	int n;
	uint32_t ea_size, ofs;

	if (blob->length < 4) {
		return NT_STATUS_INFO_LENGTH_MISMATCH;
	}

	ea_size = IVAL(blob->data, 0);
	if (ea_size > blob->length) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	ofs = 4;
	n = 0;
	*num_eas = 0;
	*eas = NULL;

	while (ofs < ea_size) {
		unsigned int len;
		DATA_BLOB blob2;

		blob2.data = blob->data + ofs;
		blob2.length = ea_size - ofs;

		*eas = talloc_realloc(mem_ctx, *eas, struct ea_struct, n+1);
		if (! *eas) return NT_STATUS_NO_MEMORY;

		len = ea_pull_struct(&blob2, mem_ctx, &(*eas)[n]);
		if (len == 0) {
			return NT_STATUS_INVALID_PARAMETER;
		}

		ofs += len;
		n++;
	}

	*num_eas = n;

	return NT_STATUS_OK;
}


/*
  pull a chained ea_list from a buffer
*/
NTSTATUS ea_pull_list_chained(const DATA_BLOB *blob, 
			      TALLOC_CTX *mem_ctx,
			      unsigned int *num_eas, struct ea_struct **eas)
{
	int n;
	uint32_t ofs;

	if (blob->length < 4) {
		return NT_STATUS_INFO_LENGTH_MISMATCH;
	}

	ofs = 0;
	n = 0;
	*num_eas = 0;
	*eas = NULL;

	while (ofs < blob->length) {
		unsigned int len;
		DATA_BLOB blob2;
		uint32_t next_ofs = IVAL(blob->data, ofs);

		blob2.data = blob->data + ofs + 4;
		blob2.length = blob->length - (ofs + 4);

		*eas = talloc_realloc(mem_ctx, *eas, struct ea_struct, n+1);
		if (! *eas) return NT_STATUS_NO_MEMORY;

		len = ea_pull_struct(&blob2, mem_ctx, &(*eas)[n]);
		if (len == 0) {
			return NT_STATUS_INVALID_PARAMETER;
		}

		ofs += next_ofs;

		if (ofs+4 > blob->length) {
			return NT_STATUS_INVALID_PARAMETER;
		}
		n++;
		if (next_ofs == 0) break;
	}

	*num_eas = n;

	return NT_STATUS_OK;
}


/*
  pull a ea_name from a buffer. Return the number of bytes consumed
*/
static unsigned int ea_pull_name(const DATA_BLOB *blob,
			   TALLOC_CTX *mem_ctx,
			   struct ea_name *ea)
{
	uint8_t nlen;

	if (blob->length < 2) {
		return 0;
	}

	nlen = CVAL(blob->data, 0);

	if (nlen+2 > blob->length) {
		return 0;
	}

	ea->name.s = talloc_strndup(mem_ctx, (const char *)(blob->data+1), nlen);
	ea->name.private_length = nlen;

	return nlen+2;
}


/*
  pull a ea_name list from a buffer
*/
NTSTATUS ea_pull_name_list(const DATA_BLOB *blob, 
			   TALLOC_CTX *mem_ctx,
			   unsigned int *num_names, struct ea_name **ea_names)
{
	int n;
	uint32_t ea_size, ofs;

	if (blob->length < 4) {
		return NT_STATUS_INFO_LENGTH_MISMATCH;
	}

	ea_size = IVAL(blob->data, 0);
	if (ea_size > blob->length) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	ofs = 4;
	n = 0;
	*num_names = 0;
	*ea_names = NULL;

	while (ofs < ea_size) {
		unsigned int len;
		DATA_BLOB blob2;

		blob2.data = blob->data + ofs;
		blob2.length = ea_size - ofs;

		*ea_names = talloc_realloc(mem_ctx, *ea_names, struct ea_name, n+1);
		if (! *ea_names) return NT_STATUS_NO_MEMORY;

		len = ea_pull_name(&blob2, mem_ctx, &(*ea_names)[n]);
		if (len == 0) {
			return NT_STATUS_INVALID_PARAMETER;
		}

		ofs += len;
		n++;
	}

	*num_names = n;

	return NT_STATUS_OK;
}


/*
  put a ea_name list into a data blob
*/
bool ea_push_name_list(TALLOC_CTX *mem_ctx,
		       DATA_BLOB *data, unsigned int num_names, struct ea_name *eas)
{
	int i;
	uint32_t ea_size;
	uint32_t off;

	ea_size = ea_name_list_size(num_names, eas);

	*data = data_blob_talloc(mem_ctx, NULL, ea_size);
	if (data->data == NULL) {
		return false;
	}

	SIVAL(data->data, 0, ea_size);
	off = 4;

	for (i=0;i<num_names;i++) {
		unsigned int nlen = strlen(eas[i].name.s);
		SCVAL(data->data, off, nlen);
		memcpy(data->data+off+1, eas[i].name.s, nlen+1);
		off += 1+nlen+1;
	}

	return true;
}