s3/lib/ctdbd_conn: assert hdr following read/recv
[Samba.git] / libcli / smb / util.c
blob6fdf35fbbf3ec80a90a32c772a13d0c74488a4bc
1 /*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2002
6 Copyright (C) James Myers 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "libcli/smb/smb_common.h"
24 #include "system/filesys.h"
26 const char *smb_protocol_types_string(enum protocol_types protocol)
28 switch (protocol) {
29 case PROTOCOL_DEFAULT:
30 return "DEFAULT";
31 case PROTOCOL_NONE:
32 return "NONE";
33 case PROTOCOL_CORE:
34 return "CORE";
35 case PROTOCOL_COREPLUS:
36 return "COREPLUS";
37 case PROTOCOL_LANMAN1:
38 return "LANMAN1";
39 case PROTOCOL_LANMAN2:
40 return "LANMAN2";
41 case PROTOCOL_NT1:
42 return "NT1";
43 case PROTOCOL_SMB2_02:
44 return "SMB2_02";
45 case PROTOCOL_SMB2_10:
46 return "SMB2_10";
47 case PROTOCOL_SMB2_22:
48 return "SMB2_22";
49 case PROTOCOL_SMB2_24:
50 return "SMB2_24";
51 case PROTOCOL_SMB3_00:
52 return "SMB3_00";
53 case PROTOCOL_SMB3_02:
54 return "SMB3_02";
55 case PROTOCOL_SMB3_10:
56 return "SMB3_10";
57 case PROTOCOL_SMB3_11:
58 return "SMB3_11";
61 return "Invalid protocol_types value";
64 /**
65 Return a string representing a CIFS attribute for a file.
66 **/
67 char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib)
69 int i, len;
70 const struct {
71 char c;
72 uint16_t attr;
73 } attr_strs[] = {
74 {'V', FILE_ATTRIBUTE_VOLUME},
75 {'D', FILE_ATTRIBUTE_DIRECTORY},
76 {'A', FILE_ATTRIBUTE_ARCHIVE},
77 {'H', FILE_ATTRIBUTE_HIDDEN},
78 {'S', FILE_ATTRIBUTE_SYSTEM},
79 {'N', FILE_ATTRIBUTE_NORMAL},
80 {'R', FILE_ATTRIBUTE_READONLY},
81 {'d', FILE_ATTRIBUTE_DEVICE},
82 {'t', FILE_ATTRIBUTE_TEMPORARY},
83 {'s', FILE_ATTRIBUTE_SPARSE},
84 {'r', FILE_ATTRIBUTE_REPARSE_POINT},
85 {'c', FILE_ATTRIBUTE_COMPRESSED},
86 {'o', FILE_ATTRIBUTE_OFFLINE},
87 {'n', FILE_ATTRIBUTE_NONINDEXED},
88 {'e', FILE_ATTRIBUTE_ENCRYPTED}
90 char *ret;
92 ret = talloc_array(mem_ctx, char, ARRAY_SIZE(attr_strs)+1);
93 if (!ret) {
94 return NULL;
97 for (len=i=0; i<ARRAY_SIZE(attr_strs); i++) {
98 if (attrib & attr_strs[i].attr) {
99 ret[len++] = attr_strs[i].c;
103 ret[len] = 0;
105 talloc_set_name_const(ret, ret);
107 return ret;
110 /****************************************************************************
111 Map standard UNIX permissions onto wire representations.
112 ****************************************************************************/
114 uint32_t unix_perms_to_wire(mode_t perms)
116 unsigned int ret = 0;
118 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
119 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
120 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
121 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
122 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
123 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
124 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
125 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
126 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
127 #ifdef S_ISVTX
128 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
129 #endif
130 #ifdef S_ISGID
131 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
132 #endif
133 #ifdef S_ISUID
134 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
135 #endif
136 return ret;
139 /****************************************************************************
140 Map wire permissions to standard UNIX.
141 ****************************************************************************/
143 mode_t wire_perms_to_unix(uint32_t perms)
145 mode_t ret = (mode_t)0;
147 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
148 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
149 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
150 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
151 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
152 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
153 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
154 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
155 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
156 #ifdef S_ISVTX
157 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
158 #endif
159 #ifdef S_ISGID
160 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
161 #endif
162 #ifdef S_ISUID
163 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
164 #endif
165 return ret;
168 /****************************************************************************
169 Return the file type from the wire filetype for UNIX extensions.
170 ****************************************************************************/
172 mode_t unix_filetype_from_wire(uint32_t wire_type)
174 switch (wire_type) {
175 case UNIX_TYPE_FILE:
176 return S_IFREG;
177 case UNIX_TYPE_DIR:
178 return S_IFDIR;
179 #ifdef S_IFLNK
180 case UNIX_TYPE_SYMLINK:
181 return S_IFLNK;
182 #endif
183 #ifdef S_IFCHR
184 case UNIX_TYPE_CHARDEV:
185 return S_IFCHR;
186 #endif
187 #ifdef S_IFBLK
188 case UNIX_TYPE_BLKDEV:
189 return S_IFBLK;
190 #endif
191 #ifdef S_IFIFO
192 case UNIX_TYPE_FIFO:
193 return S_IFIFO;
194 #endif
195 #ifdef S_IFSOCK
196 case UNIX_TYPE_SOCKET:
197 return S_IFSOCK;
198 #endif
199 default:
200 return (mode_t)0;
204 bool smb_buffer_oob(uint32_t bufsize, uint32_t offset, uint32_t length)
206 if ((offset + length < offset) || (offset + length < length)) {
207 /* wrap */
208 return true;
210 if ((offset > bufsize) || (offset + length > bufsize)) {
211 /* overflow */
212 return true;
214 return false;
217 /***********************************************************
218 Common function for pushing stings, used by smb_bytes_push_str()
219 and trans_bytes_push_str(). Only difference is the align_odd
220 parameter setting.
221 ***********************************************************/
223 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
224 const char *str, size_t str_len,
225 bool align_odd,
226 size_t *pconverted_size)
228 TALLOC_CTX *frame = talloc_stackframe();
229 size_t buflen;
230 char *converted;
231 size_t converted_size;
234 * This check prevents us from
235 * (re)alloc buf on a NULL TALLOC_CTX.
237 if (buf == NULL) {
238 TALLOC_FREE(frame);
239 return NULL;
242 buflen = talloc_get_size(buf);
244 if (ucs2 &&
245 ((align_odd && (buflen % 2 == 0)) ||
246 (!align_odd && (buflen % 2 == 1)))) {
248 * We're pushing into an SMB buffer, align odd
250 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
251 if (buf == NULL) {
252 TALLOC_FREE(frame);
253 return NULL;
255 buf[buflen] = '\0';
256 buflen += 1;
259 if (!convert_string_talloc(frame, CH_UNIX,
260 ucs2 ? CH_UTF16LE : CH_DOS,
261 str, str_len, &converted,
262 &converted_size)) {
263 TALLOC_FREE(frame);
264 return NULL;
267 buf = talloc_realloc(NULL, buf, uint8_t,
268 buflen + converted_size);
269 if (buf == NULL) {
270 TALLOC_FREE(frame);
271 return NULL;
274 memcpy(buf + buflen, converted, converted_size);
276 TALLOC_FREE(converted);
278 if (pconverted_size) {
279 *pconverted_size = converted_size;
282 TALLOC_FREE(frame);
283 return buf;
286 /***********************************************************
287 Push a string into an SMB buffer, with odd byte alignment
288 if it's a UCS2 string.
289 ***********************************************************/
291 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
292 const char *str, size_t str_len,
293 size_t *pconverted_size)
295 return internal_bytes_push_str(buf, ucs2, str, str_len,
296 true, pconverted_size);
299 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
300 const uint8_t *bytes, size_t num_bytes)
302 size_t buflen;
305 * This check prevents us from
306 * (re)alloc buf on a NULL TALLOC_CTX.
308 if (buf == NULL) {
309 return NULL;
311 buflen = talloc_get_size(buf);
313 buf = talloc_realloc(NULL, buf, uint8_t,
314 buflen + 1 + num_bytes);
315 if (buf == NULL) {
316 return NULL;
318 buf[buflen] = prefix;
319 memcpy(&buf[buflen+1], bytes, num_bytes);
320 return buf;
323 /***********************************************************
324 Same as smb_bytes_push_str(), but without the odd byte
325 align for ucs2 (we're pushing into a param or data block).
326 static for now, although this will probably change when
327 other modules use async trans calls.
328 ***********************************************************/
330 uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
331 const char *str, size_t str_len,
332 size_t *pconverted_size)
334 return internal_bytes_push_str(buf, ucs2, str, str_len,
335 false, pconverted_size);
338 uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
339 const uint8_t *bytes, size_t num_bytes)
341 size_t buflen;
343 if (buf == NULL) {
344 return NULL;
346 buflen = talloc_get_size(buf);
348 buf = talloc_realloc(NULL, buf, uint8_t,
349 buflen + num_bytes);
350 if (buf == NULL) {
351 return NULL;
353 memcpy(&buf[buflen], bytes, num_bytes);
354 return buf;
357 static NTSTATUS internal_bytes_pull_str(TALLOC_CTX *mem_ctx, char **_str,
358 bool ucs2, bool align_odd,
359 const uint8_t *buf, size_t buf_len,
360 const uint8_t *position,
361 size_t *p_consumed)
363 size_t pad = 0;
364 size_t offset;
365 char *str = NULL;
366 size_t str_len = 0;
367 bool ok;
369 *_str = NULL;
370 if (p_consumed != NULL) {
371 *p_consumed = 0;
374 if (position < buf) {
375 return NT_STATUS_INTERNAL_ERROR;
378 offset = PTR_DIFF(position, buf);
379 if (offset > buf_len) {
380 return NT_STATUS_BUFFER_TOO_SMALL;
383 if (ucs2 &&
384 ((align_odd && (offset % 2 == 0)) ||
385 (!align_odd && (offset % 2 == 1)))) {
386 pad += 1;
387 offset += 1;
390 if (offset > buf_len) {
391 return NT_STATUS_BUFFER_TOO_SMALL;
394 buf_len -= offset;
395 buf += offset;
397 if (ucs2) {
398 buf_len = utf16_len_n(buf, buf_len);
399 } else {
400 size_t tmp = strnlen((const char *)buf, buf_len);
401 if (tmp < buf_len) {
402 tmp += 1;
404 buf_len = tmp;
407 ok = convert_string_talloc(mem_ctx,
408 ucs2 ? CH_UTF16LE : CH_DOS,
409 CH_UNIX,
410 buf, buf_len,
411 &str, &str_len);
412 if (!ok) {
413 return map_nt_error_from_unix_common(errno);
416 if (p_consumed != NULL) {
417 *p_consumed = buf_len + pad;
419 *_str = str;
420 return NT_STATUS_OK;
423 NTSTATUS smb_bytes_pull_str(TALLOC_CTX *mem_ctx, char **_str, bool ucs2,
424 const uint8_t *buf, size_t buf_len,
425 const uint8_t *position,
426 size_t *_consumed)
428 return internal_bytes_pull_str(mem_ctx, _str, ucs2, true,
429 buf, buf_len, position, _consumed);