smb_file *smb_find(smb_session *s, smb_tid tid, const char *pattern) { smb_message *msg; smb_trans2_req *tr2; smb_tr2_find2 *find; size_t pattern_len, msg_len; int res; assert(s != NULL && pattern != NULL && tid); pattern_len = strlen(pattern) + 1; msg_len = sizeof(smb_trans2_req) + sizeof(smb_tr2_find2); msg_len += pattern_len * 2 + 3; msg = smb_message_new(SMB_CMD_TRANS2, msg_len); smb_message_set_default_flags(msg); msg->packet->header.tid = tid; tr2 = (smb_trans2_req *)msg->packet->payload; tr2->wct = 15; tr2->total_param_count = pattern_len * 2 + sizeof(smb_tr2_find2); tr2->max_param_count = 10; // ?? Why not the same or 12 ? tr2->max_data_count = 0xffff; tr2->param_count = tr2->total_param_count; tr2->param_offset = 68; // Offset of find_first_params in packet; tr2->data_count = 0; tr2->data_offset = 88; // Offset of pattern in packet tr2->setup_count = 1; tr2->cmd = SMB_TR2_FIND_FIRST; tr2->bct = sizeof(smb_tr2_find2) + pattern_len * 2 + 3; find = (smb_tr2_find2 *) tr2->payload; find->attrs = SMB_FIND2_ATTR_DEFAULT; find->count = 1366; // ?? find->flags = SMB_FIND2_FLAG_DEFAULT; find->interest = 0x0104; // 'Find file both directory info' smb_message_advance(msg, sizeof(smb_trans2_req)); smb_message_advance(msg, sizeof(smb_tr2_find2)); smb_message_put_utf16(msg, "", pattern, pattern_len); // Adds padding at the end if necessary. if (tr2->bct % 4) smb_message_advance(msg, 4 - tr2->bct % 4); res = smb_session_send_msg(s, msg); smb_message_destroy(msg); if (!res) { BDSM_dbg("Unable to query pattern: %s\n", pattern); return (NULL); } if ((msg = smb_tr2_recv(s)) == NULL) return (NULL); return (smb_find_parse(msg)); }
static smb_message *smb_trans2_find_next (smb_session *s, smb_tid tid, uint16_t resume_key, uint16_t sid, const char *pattern) { smb_message *msg_find_next2 = NULL; smb_trans2_req tr2_find_next2; smb_tr2_findnext2 find_next2; size_t utf_pattern_len, tr2_bct, tr2_param_count; char *utf_pattern; int res; unsigned int padding = 0; assert(s != NULL && pattern != NULL); utf_pattern_len = smb_to_utf16(pattern, strlen(pattern) + 1, &utf_pattern); if (utf_pattern_len == 0) return NULL; tr2_bct = sizeof(smb_tr2_findnext2) + utf_pattern_len; tr2_param_count = tr2_bct; tr2_bct += 3; // Adds padding at the end if necessary. while ((tr2_bct % 4) != 3) { padding++; tr2_bct++; } msg_find_next2 = smb_message_new(SMB_CMD_TRANS2); if (!msg_find_next2) { free(utf_pattern); return NULL; } msg_find_next2->packet->header.tid = tid; SMB_MSG_INIT_PKT(tr2_find_next2); tr2_find_next2.wct = 0x0f; tr2_find_next2.total_param_count = tr2_param_count; tr2_find_next2.total_data_count = 0x0000; tr2_find_next2.max_param_count = 10; // ?? Why not the same or 12 ? tr2_find_next2.max_data_count = 0xffff; //max_setup_count //reserved //flags //timeout //reserve2 tr2_find_next2.param_count = tr2_param_count; tr2_find_next2.param_offset = 68; // Offset of find_next_params in packet; tr2_find_next2.data_count = 0; tr2_find_next2.data_offset = 88; // Offset of pattern in packet tr2_find_next2.setup_count = 1; //reserve3 tr2_find_next2.cmd = SMB_TR2_FIND_NEXT; tr2_find_next2.bct = tr2_bct; //3 == padding SMB_MSG_PUT_PKT(msg_find_next2, tr2_find_next2); SMB_MSG_INIT_PKT(find_next2); find_next2.sid = sid; find_next2.count = 255; find_next2.interest = SMB_FIND2_INTEREST_BOTH_DIRECTORY_INFO; find_next2.flags = SMB_FIND2_FLAG_CLOSE_EOS|SMB_FIND2_FLAG_CONTINUE; find_next2.resume_key = resume_key; SMB_MSG_PUT_PKT(msg_find_next2, find_next2); smb_message_append(msg_find_next2, utf_pattern, utf_pattern_len); while (padding--) smb_message_put8(msg_find_next2, 0); res = smb_session_send_msg(s, msg_find_next2); smb_message_destroy(msg_find_next2); free(utf_pattern); if (!res) { BDSM_dbg("Unable to query pattern: %s\n", pattern); return NULL; } msg_find_next2 = smb_tr2_recv(s); return msg_find_next2; }
static smb_message *smb_trans2_find_first (smb_session *s, smb_tid tid, const char *pattern) { smb_message *msg; smb_trans2_req tr2; smb_tr2_findfirst2 find; size_t utf_pattern_len, tr2_bct, tr2_param_count; char *utf_pattern; int res; unsigned int padding = 0; assert(s != NULL && pattern != NULL); utf_pattern_len = smb_to_utf16(pattern, strlen(pattern) + 1, &utf_pattern); if (utf_pattern_len == 0) return NULL; tr2_bct = sizeof(smb_tr2_findfirst2) + utf_pattern_len; tr2_param_count = tr2_bct; tr2_bct += 3; // Adds padding at the end if necessary. while ((tr2_bct % 4) != 3) { padding++; tr2_bct++; } msg = smb_message_new(SMB_CMD_TRANS2); if (!msg) { free(utf_pattern); return NULL; } msg->packet->header.tid = tid; SMB_MSG_INIT_PKT(tr2); tr2.wct = 15; tr2.max_param_count = 10; // ?? Why not the same or 12 ? tr2.max_data_count = 0xffff;; tr2.param_offset = 68; // Offset of find_first_params in packet; tr2.data_count = 0; tr2.data_offset = 88; // Offset of pattern in packet tr2.setup_count = 1; tr2.cmd = SMB_TR2_FIND_FIRST; tr2.total_param_count = tr2_param_count; tr2.param_count = tr2_param_count; tr2.bct = tr2_bct; //3 == padding SMB_MSG_PUT_PKT(msg, tr2); SMB_MSG_INIT_PKT(find); find.attrs = SMB_FIND2_ATTR_DEFAULT; find.count = 1366; // ?? find.flags = SMB_FIND2_FLAG_CLOSE_EOS | SMB_FIND2_FLAG_RESUME; find.interest = SMB_FIND2_INTEREST_BOTH_DIRECTORY_INFO; SMB_MSG_PUT_PKT(msg, find); smb_message_append(msg, utf_pattern, utf_pattern_len); while (padding--) smb_message_put8(msg, 0); res = smb_session_send_msg(s, msg); smb_message_destroy(msg); free(utf_pattern); if (!res) { BDSM_dbg("Unable to query pattern: %s\n", pattern); return NULL; } msg = smb_tr2_recv(s); return msg; }