source: trunk/server/libcli/smb/smb2_create_blob.c

Last change on this file was 752, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.9 2nd part

File size: 5.7 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 SMB2 Create Context Blob handling
5
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Stefan Metzmacher 2008-2009
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "includes.h"
24#include "../libcli/smb/smb_common.h"
25
26static size_t smb2_create_blob_padding(uint32_t offset, size_t n)
27{
28 if ((offset & (n-1)) == 0) return 0;
29 return n - (offset & (n-1));
30}
31
32/*
33 parse a set of SMB2 create blobs
34*/
35NTSTATUS smb2_create_blob_parse(TALLOC_CTX *mem_ctx, const DATA_BLOB buffer,
36 struct smb2_create_blobs *blobs)
37{
38 const uint8_t *data = buffer.data;
39 uint32_t remaining = buffer.length;
40
41 while (remaining > 0) {
42 uint32_t next;
43 uint32_t name_offset, name_length;
44 uint32_t reserved, data_offset;
45 uint32_t data_length;
46 char *tag;
47 DATA_BLOB b;
48 NTSTATUS status;
49
50 if (remaining < 16) {
51 return NT_STATUS_INVALID_PARAMETER;
52 }
53 next = IVAL(data, 0);
54 name_offset = SVAL(data, 4);
55 name_length = SVAL(data, 6);
56 reserved = SVAL(data, 8);
57 data_offset = SVAL(data, 10);
58 data_length = IVAL(data, 12);
59
60 if ((next & 0x7) != 0 ||
61 next > remaining ||
62 name_offset != 16 ||
63 name_length < 4 ||
64 name_offset + name_length > remaining ||
65 (data_offset & 0x7) != 0 ||
66 (data_offset && (data_offset < name_offset + name_length)) ||
67 (data_offset > remaining) ||
68 (data_offset + (uint64_t)data_length > remaining)) {
69 return NT_STATUS_INVALID_PARAMETER;
70 }
71
72 tag = talloc_strndup(mem_ctx, (const char *)data + name_offset, name_length);
73 if (tag == NULL) {
74 return NT_STATUS_NO_MEMORY;
75 }
76
77 b = data_blob_const(data+data_offset, data_length);
78 status = smb2_create_blob_add(mem_ctx, blobs, tag, b);
79 if (!NT_STATUS_IS_OK(status)) {
80 return status;
81 }
82
83 talloc_free(tag);
84
85 if (next == 0) break;
86
87 remaining -= next;
88 data += next;
89
90 if (remaining < 16) {
91 return NT_STATUS_INVALID_PARAMETER;
92 }
93 }
94
95 return NT_STATUS_OK;
96}
97
98
99/*
100 add a blob to a smb2_create attribute blob
101*/
102static NTSTATUS smb2_create_blob_push_one(TALLOC_CTX *mem_ctx, DATA_BLOB *buffer,
103 const struct smb2_create_blob *blob,
104 bool last)
105{
106 uint32_t ofs = buffer->length;
107 size_t tag_length = strlen(blob->tag);
108 size_t blob_offset = 0;
109 size_t blob_pad = 0;
110 size_t next_offset = 0;
111 size_t next_pad = 0;
112 bool ok;
113
114 blob_offset = 0x10 + tag_length;
115 blob_pad = smb2_create_blob_padding(blob_offset, 8);
116 next_offset = blob_offset + blob_pad + blob->data.length;
117 if (!last) {
118 next_pad = smb2_create_blob_padding(next_offset, 8);
119 }
120
121 ok = data_blob_realloc(mem_ctx, buffer,
122 buffer->length + next_offset + next_pad);
123 if (!ok) {
124 return NT_STATUS_NO_MEMORY;
125 }
126
127 if (last) {
128 SIVAL(buffer->data, ofs+0x00, 0);
129 } else {
130 SIVAL(buffer->data, ofs+0x00, next_offset + next_pad);
131 }
132 SSVAL(buffer->data, ofs+0x04, 0x10); /* offset of tag */
133 SIVAL(buffer->data, ofs+0x06, tag_length); /* tag length */
134 SSVAL(buffer->data, ofs+0x0A, blob_offset + blob_pad); /* offset of data */
135 SIVAL(buffer->data, ofs+0x0C, blob->data.length);
136 memcpy(buffer->data+ofs+0x10, blob->tag, tag_length);
137 if (blob_pad > 0) {
138 memset(buffer->data+ofs+blob_offset, 0, blob_pad);
139 blob_offset += blob_pad;
140 }
141 memcpy(buffer->data+ofs+blob_offset, blob->data.data, blob->data.length);
142 if (next_pad > 0) {
143 memset(buffer->data+ofs+next_offset, 0, next_pad);
144 next_offset += next_pad;
145 }
146
147 return NT_STATUS_OK;
148}
149
150
151/*
152 create a buffer of a set of create blobs
153*/
154NTSTATUS smb2_create_blob_push(TALLOC_CTX *mem_ctx, DATA_BLOB *buffer,
155 const struct smb2_create_blobs blobs)
156{
157 int i;
158 NTSTATUS status;
159
160 *buffer = data_blob(NULL, 0);
161 for (i=0; i < blobs.num_blobs; i++) {
162 bool last = false;
163 const struct smb2_create_blob *c;
164
165 if ((i + 1) == blobs.num_blobs) {
166 last = true;
167 }
168
169 c = &blobs.blobs[i];
170 status = smb2_create_blob_push_one(mem_ctx, buffer, c, last);
171 if (!NT_STATUS_IS_OK(status)) {
172 return status;
173 }
174 }
175 return NT_STATUS_OK;
176}
177
178
179NTSTATUS smb2_create_blob_add(TALLOC_CTX *mem_ctx, struct smb2_create_blobs *b,
180 const char *tag, DATA_BLOB data)
181{
182 struct smb2_create_blob *array;
183
184 array = talloc_realloc(mem_ctx, b->blobs,
185 struct smb2_create_blob,
186 b->num_blobs + 1);
187 NT_STATUS_HAVE_NO_MEMORY(array);
188 b->blobs = array;
189
190 b->blobs[b->num_blobs].tag = talloc_strdup(b->blobs, tag);
191 NT_STATUS_HAVE_NO_MEMORY(b->blobs[b->num_blobs].tag);
192
193 if (data.data) {
194 b->blobs[b->num_blobs].data = data_blob_talloc(b->blobs,
195 data.data,
196 data.length);
197 NT_STATUS_HAVE_NO_MEMORY(b->blobs[b->num_blobs].data.data);
198 } else {
199 b->blobs[b->num_blobs].data = data_blob(NULL, 0);
200 }
201
202 b->num_blobs += 1;
203
204 return NT_STATUS_OK;
205}
206
207/*
208 * return the first blob with the given tag
209 */
210struct smb2_create_blob *smb2_create_blob_find(const struct smb2_create_blobs *b,
211 const char *tag)
212{
213 uint32_t i;
214
215 for (i=0; i < b->num_blobs; i++) {
216 if (strcmp(b->blobs[i].tag, tag) == 0) {
217 return &b->blobs[i];
218 }
219 }
220
221 return NULL;
222}
Note: See TracBrowser for help on using the repository browser.