📄 request.c
字号:
/* push a uint16_t ofs/ uint32_t length/blob triple into a data blob the ofs points to the start of the offset/length pair, and is relative to the body start*/NTSTATUS smb2_push_o16s32_blob(struct smb2_request_buffer *buf, uint16_t ofs, DATA_BLOB blob){ NTSTATUS status; size_t offset; size_t padding_length; size_t padding_fix; uint8_t *ptr = buf->body+ofs; if (buf->dynamic == NULL) { return NT_STATUS_INVALID_PARAMETER; } /* check if there're enough room for ofs and size */ if (smb2_oob(buf, ptr, 6)) { return NT_STATUS_BUFFER_TOO_SMALL; } if (blob.data == NULL) { if (blob.length != 0) { return NT_STATUS_INTERNAL_ERROR; } SSVAL(ptr, 0, 0); SIVAL(ptr, 2, 0); return NT_STATUS_OK; } offset = buf->dynamic - buf->hdr; padding_length = smb2_padding_size(offset, 2); offset += padding_length; padding_fix = smb2_padding_fix(buf); SSVAL(ptr, 0, offset); SIVAL(ptr, 2, blob.length); status = smb2_grow_buffer(buf, blob.length + padding_length - padding_fix); NT_STATUS_NOT_OK_RETURN(status); memset(buf->dynamic, 0, padding_length); buf->dynamic += padding_length; memcpy(buf->dynamic, blob.data, blob.length); buf->dynamic += blob.length; buf->size += blob.length + padding_length - padding_fix; buf->body_size += blob.length + padding_length; return NT_STATUS_OK;}/* push a uint32_t ofs/ uint32_t length/blob triple into a data blob the ofs points to the start of the offset/length pair, and is relative to the body start*/NTSTATUS smb2_push_o32s32_blob(struct smb2_request_buffer *buf, uint32_t ofs, DATA_BLOB blob){ NTSTATUS status; size_t offset; size_t padding_length; size_t padding_fix; uint8_t *ptr = buf->body+ofs; if (buf->dynamic == NULL) { return NT_STATUS_INVALID_PARAMETER; } /* check if there're enough room for ofs and size */ if (smb2_oob(buf, ptr, 8)) { return NT_STATUS_BUFFER_TOO_SMALL; } if (blob.data == NULL) { if (blob.length != 0) { return NT_STATUS_INTERNAL_ERROR; } SIVAL(ptr, 0, 0); SIVAL(ptr, 4, 0); return NT_STATUS_OK; } offset = buf->dynamic - buf->hdr; padding_length = smb2_padding_size(offset, 8); offset += padding_length; padding_fix = smb2_padding_fix(buf); SIVAL(ptr, 0, offset); SIVAL(ptr, 4, blob.length); status = smb2_grow_buffer(buf, blob.length + padding_length - padding_fix); NT_STATUS_NOT_OK_RETURN(status); memset(buf->dynamic, 0, padding_length); buf->dynamic += padding_length; memcpy(buf->dynamic, blob.data, blob.length); buf->dynamic += blob.length; buf->size += blob.length + padding_length - padding_fix; buf->body_size += blob.length + padding_length; return NT_STATUS_OK;}/* push a uint32_t length/ uint32_t ofs/blob triple into a data blob the ofs points to the start of the length/offset pair, and is relative to the body start*/NTSTATUS smb2_push_s32o32_blob(struct smb2_request_buffer *buf, uint32_t ofs, DATA_BLOB blob){ NTSTATUS status; size_t offset; size_t padding_length; size_t padding_fix; uint8_t *ptr = buf->body+ofs; if (buf->dynamic == NULL) { return NT_STATUS_INVALID_PARAMETER; } /* check if there're enough room for ofs and size */ if (smb2_oob(buf, ptr, 8)) { return NT_STATUS_BUFFER_TOO_SMALL; } if (blob.data == NULL) { if (blob.length != 0) { return NT_STATUS_INTERNAL_ERROR; } SIVAL(ptr, 0, 0); SIVAL(ptr, 4, 0); return NT_STATUS_OK; } offset = buf->dynamic - buf->hdr; padding_length = smb2_padding_size(offset, 8); offset += padding_length; padding_fix = smb2_padding_fix(buf); SIVAL(ptr, 0, blob.length); SIVAL(ptr, 4, offset); status = smb2_grow_buffer(buf, blob.length + padding_length - padding_fix); NT_STATUS_NOT_OK_RETURN(status); memset(buf->dynamic, 0, padding_length); buf->dynamic += padding_length; memcpy(buf->dynamic, blob.data, blob.length); buf->dynamic += blob.length; buf->size += blob.length + padding_length - padding_fix; buf->body_size += blob.length + padding_length; return NT_STATUS_OK;}/* pull a uint16_t ofs/ uint32_t length/blob triple from a data blob the ptr points to the start of the offset/length pair*/NTSTATUS smb2_pull_o16s32_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob){ uint16_t ofs; uint32_t size; if (smb2_oob(buf, ptr, 6)) { return NT_STATUS_BUFFER_TOO_SMALL; } ofs = SVAL(ptr, 0); size = IVAL(ptr, 2); if (ofs == 0) { *blob = data_blob(NULL, 0); return NT_STATUS_OK; } if (smb2_oob(buf, buf->hdr + ofs, size)) { return NT_STATUS_BUFFER_TOO_SMALL; } *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size); NT_STATUS_HAVE_NO_MEMORY(blob->data); return NT_STATUS_OK;}/* pull a uint32_t ofs/ uint32_t length/blob triple from a data blob the ptr points to the start of the offset/length pair*/NTSTATUS smb2_pull_o32s32_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob){ uint32_t ofs, size; if (smb2_oob(buf, ptr, 8)) { return NT_STATUS_BUFFER_TOO_SMALL; } ofs = IVAL(ptr, 0); size = IVAL(ptr, 4); if (ofs == 0) { *blob = data_blob(NULL, 0); return NT_STATUS_OK; } if (smb2_oob(buf, buf->hdr + ofs, size)) { return NT_STATUS_BUFFER_TOO_SMALL; } *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size); NT_STATUS_HAVE_NO_MEMORY(blob->data); return NT_STATUS_OK;}/* pull a uint16_t ofs/ uint32_t length/blob triple from a data blob the ptr points to the start of the offset/length pair In this varient the uint16_t is padded by an extra 2 bytes, making the size aligned on 4 byte boundary*/NTSTATUS smb2_pull_o16As32_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob){ uint32_t ofs, size; if (smb2_oob(buf, ptr, 8)) { return NT_STATUS_BUFFER_TOO_SMALL; } ofs = SVAL(ptr, 0); size = IVAL(ptr, 4); if (ofs == 0) { *blob = data_blob(NULL, 0); return NT_STATUS_OK; } if (smb2_oob(buf, buf->hdr + ofs, size)) { return NT_STATUS_BUFFER_TOO_SMALL; } *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size); NT_STATUS_HAVE_NO_MEMORY(blob->data); return NT_STATUS_OK;}/* pull a uint32_t length/ uint32_t ofs/blob triple from a data blob the ptr points to the start of the offset/length pair*/NTSTATUS smb2_pull_s32o32_blob(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, DATA_BLOB *blob){ uint32_t ofs, size; if (smb2_oob(buf, ptr, 8)) { return NT_STATUS_BUFFER_TOO_SMALL; } size = IVAL(ptr, 0); ofs = IVAL(ptr, 4); if (ofs == 0) { *blob = data_blob(NULL, 0); return NT_STATUS_OK; } if (smb2_oob(buf, buf->hdr + ofs, size)) { return NT_STATUS_BUFFER_TOO_SMALL; } *blob = data_blob_talloc(mem_ctx, buf->hdr + ofs, size); NT_STATUS_HAVE_NO_MEMORY(blob->data); return NT_STATUS_OK;}/* pull a string in a uint16_t ofs/ uint16_t length/blob format UTF-16 without termination*/NTSTATUS smb2_pull_o16s16_string(struct smb2_request_buffer *buf, TALLOC_CTX *mem_ctx, uint8_t *ptr, const char **str){ DATA_BLOB blob; NTSTATUS status; ssize_t size; void *vstr; status = smb2_pull_o16s16_blob(buf, mem_ctx, ptr, &blob); NT_STATUS_NOT_OK_RETURN(status); if (blob.data == NULL) { *str = NULL; return NT_STATUS_OK; } if (blob.length == 0) { char *s; s = talloc_strdup(mem_ctx, ""); NT_STATUS_HAVE_NO_MEMORY(s); *str = s; return NT_STATUS_OK; } size = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX, blob.data, blob.length, &vstr); data_blob_free(&blob); (*str) = (char *)vstr; if (size == -1) { return NT_STATUS_ILLEGAL_CHARACTER; } return NT_STATUS_OK;}/* push a string in a uint16_t ofs/ uint16_t length/blob format UTF-16 without termination*/NTSTATUS smb2_push_o16s16_string(struct smb2_request_buffer *buf, uint16_t ofs, const char *str){ DATA_BLOB blob; NTSTATUS status; ssize_t size; if (str == NULL) { return smb2_push_o16s16_blob(buf, ofs, data_blob(NULL, 0)); } if (*str == 0) { blob.data = discard_const(str); blob.length = 0; return smb2_push_o16s16_blob(buf, ofs, blob); } size = convert_string_talloc(buf->buffer, lp_iconv_convenience(global_loadparm), CH_UNIX, CH_UTF16, str, strlen(str), (void **)&blob.data); if (size == -1) { return NT_STATUS_ILLEGAL_CHARACTER; } blob.length = size; status = smb2_push_o16s16_blob(buf, ofs, blob); data_blob_free(&blob); return status;}/* push a file handle into a buffer*/void smb2_push_handle(uint8_t *data, struct smb2_handle *h){ SBVAL(data, 0, h->data[0]); SBVAL(data, 8, h->data[1]);}/* pull a file handle from a buffer*/void smb2_pull_handle(uint8_t *ptr, struct smb2_handle *h){ h->data[0] = BVAL(ptr, 0); h->data[1] = BVAL(ptr, 8);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -