smb_trans2.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*****************************************************************************
  2. * __________________ _________ _____ _____ .__ ._.
  3. * \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
  4. * | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
  5. * | | \| ` \/ / Y \ / | | \ ___/ \|
  6. * |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
  7. * \/ \/ \/ \/ )/ \/ \/ \/
  8. *
  9. * This file is part of liBDSM. Copyright © 2014-2015 VideoLabs SAS
  10. *
  11. * Author: Julien 'Lta' BALLET <contact@lta.io>
  12. *
  13. * liBDSM is released under LGPLv2.1 (or later) and is also available
  14. * under a commercial license.
  15. *****************************************************************************
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU Lesser General Public License as published by
  18. * the Free Software Foundation; either version 2.1 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Lesser General Public License
  27. * along with this program; if not, write to the Free Software Foundation,
  28. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  29. *****************************************************************************/
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include <assert.h>
  34. #include "bdsm_debug.h"
  35. #include "smb_message.h"
  36. #include "smb_session_msg.h"
  37. #include "smb_utils.h"
  38. #include "smb_stat.h"
  39. static smb_message *smb_tr2_recv(smb_session *s)
  40. {
  41. smb_message recv, *res;
  42. smb_trans2_resp *tr2;
  43. size_t growth;
  44. int remaining;
  45. if (!smb_session_recv_msg(s, &recv))
  46. return (NULL);
  47. tr2 = (smb_trans2_resp *)recv.packet->payload;
  48. growth = tr2->total_data_count - tr2->data_count;
  49. res = smb_message_grow(&recv, growth);
  50. if (!res)
  51. return (NULL);
  52. res->cursor = recv.payload_size;
  53. remaining = (int)tr2->total_data_count -
  54. (tr2->data_displacement + tr2->data_count);
  55. while (remaining > 0)
  56. {
  57. remaining = smb_session_recv_msg(s, &recv);
  58. if (remaining)
  59. {
  60. tr2 = (smb_trans2_resp *)recv.packet->payload;
  61. /*
  62. * XXX: Why does padding was necessary and is not anymore, i.e.
  63. * find a reproductible setup where it is
  64. */
  65. smb_message_append(res, tr2->payload /* + 2 pad */, tr2->data_count);
  66. remaining = (int)tr2->total_data_count -
  67. (tr2->data_displacement + tr2->data_count);
  68. }
  69. }
  70. return (res);
  71. }
  72. static void smb_tr2_find2_parse_entries(smb_file **files_p, smb_tr2_find2_entry *iter, size_t count, uint8_t *eod)
  73. {
  74. smb_file *tmp = NULL;
  75. size_t i;
  76. for (i = 0; i < count && (uint8_t *)iter < eod; i++)
  77. {
  78. // Create a smb_file and fill it
  79. tmp = calloc(1, sizeof(smb_file));
  80. if (!tmp)
  81. return;
  82. tmp->name_len = smb_from_utf16((const char *)iter->name, iter->name_len,
  83. &tmp->name);
  84. tmp->name[tmp->name_len] = 0;
  85. tmp->created = iter->created;
  86. tmp->accessed = iter->accessed;
  87. tmp->written = iter->written;
  88. tmp->changed = iter->changed;
  89. tmp->size = iter->size;
  90. tmp->alloc_size = iter->alloc_size;
  91. tmp->attr = iter->attr;
  92. tmp->is_dir = tmp->attr & SMB_ATTR_DIR;
  93. tmp->next = *files_p;
  94. *files_p = tmp;
  95. iter = (smb_tr2_find2_entry *)(((char *)iter) + iter->next_entry);
  96. }
  97. return;
  98. }
  99. static void smb_find_first_parse(smb_message *msg, smb_file **files_p)
  100. {
  101. smb_trans2_resp *tr2;
  102. smb_tr2_findfirst2_params *params;
  103. smb_tr2_find2_entry *iter;
  104. uint8_t *eod;
  105. size_t count;
  106. assert(msg != NULL);
  107. // Let's parse the answer we got from server
  108. tr2 = (smb_trans2_resp *)msg->packet->payload;
  109. params = (smb_tr2_findfirst2_params *)tr2->payload;
  110. iter = (smb_tr2_find2_entry *)(tr2->payload + sizeof(smb_tr2_findfirst2_params));
  111. eod = msg->packet->payload + msg->payload_size;
  112. count = params->count;
  113. smb_tr2_find2_parse_entries(files_p, iter, count, eod);
  114. return;
  115. }
  116. static smb_message *smb_trans2_find_first (smb_session *s, smb_tid tid, const char *pattern)
  117. {
  118. smb_message *msg;
  119. smb_trans2_req tr2;
  120. smb_tr2_findfirst2 find;
  121. size_t utf_pattern_len, tr2_bct, tr2_param_count;
  122. char *utf_pattern;
  123. int res;
  124. unsigned int padding = 0;
  125. assert(s != NULL && pattern != NULL && tid);
  126. utf_pattern_len = smb_to_utf16(pattern, strlen(pattern) + 1, &utf_pattern);
  127. if (utf_pattern_len == 0)
  128. return (NULL);
  129. tr2_bct = sizeof(smb_tr2_findfirst2) + utf_pattern_len;
  130. tr2_param_count = tr2_bct;
  131. tr2_bct += 3;
  132. // Adds padding at the end if necessary.
  133. while ((tr2_bct % 4) != 3)
  134. {
  135. padding++;
  136. tr2_bct++;
  137. }
  138. msg = smb_message_new(SMB_CMD_TRANS2);
  139. if (!msg) {
  140. free(utf_pattern);
  141. return (NULL);
  142. }
  143. msg->packet->header.tid = tid;
  144. SMB_MSG_INIT_PKT(tr2);
  145. tr2.wct = 15;
  146. tr2.max_param_count = 10; // ?? Why not the same or 12 ?
  147. tr2.max_data_count = 0xffff;;
  148. tr2.param_offset = 68; // Offset of find_first_params in packet;
  149. tr2.data_count = 0;
  150. tr2.data_offset = 88; // Offset of pattern in packet
  151. tr2.setup_count = 1;
  152. tr2.cmd = SMB_TR2_FIND_FIRST;
  153. tr2.total_param_count = tr2_param_count;
  154. tr2.param_count = tr2_param_count;
  155. tr2.bct = tr2_bct; //3 == padding
  156. SMB_MSG_PUT_PKT(msg, tr2);
  157. SMB_MSG_INIT_PKT(find);
  158. find.attrs = SMB_FIND2_ATTR_DEFAULT;
  159. find.count = 1366; // ??
  160. find.flags = SMB_FIND2_FLAG_DEFAULT | SMB_FIND2_FLAG_CLOSE;
  161. find.interest = 0x0104; // 'Find file both directory info'
  162. SMB_MSG_PUT_PKT(msg, find);
  163. smb_message_append(msg, utf_pattern, utf_pattern_len);
  164. while (padding--)
  165. smb_message_put8(msg, 0);
  166. res = smb_session_send_msg(s, msg);
  167. smb_message_destroy(msg);
  168. free(utf_pattern);
  169. if (!res)
  170. {
  171. BDSM_dbg("Unable to query pattern: %s\n", pattern);
  172. return (NULL);
  173. }
  174. msg = smb_tr2_recv(s);
  175. return msg;
  176. }
  177. smb_file *smb_find(smb_session *s, smb_tid tid, const char *pattern)
  178. {
  179. smb_file *files = NULL;
  180. smb_message *msg;
  181. assert(s != NULL && pattern != NULL && tid);
  182. msg = smb_trans2_find_first(s,tid,pattern);
  183. if (msg != NULL)
  184. {
  185. smb_find_first_parse(msg,&files);
  186. smb_message_destroy(msg);
  187. }
  188. return (files);
  189. }
  190. smb_file *smb_fstat(smb_session *s, smb_tid tid, const char *path)
  191. {
  192. smb_message *msg, reply;
  193. smb_trans2_req tr2;
  194. smb_trans2_resp *tr2_resp;
  195. smb_tr2_query query;
  196. smb_tr2_path_info *info;
  197. smb_file *file;
  198. size_t utf_path_len, msg_len;
  199. char *utf_path;
  200. int res, padding = 0;
  201. assert(s != NULL && path != NULL && tid);
  202. utf_path_len = smb_to_utf16(path, strlen(path) + 1, &utf_path);
  203. if (utf_path_len == 0)
  204. return (0);
  205. msg_len = sizeof(smb_trans2_req) + sizeof(smb_tr2_query);
  206. msg_len += utf_path_len;
  207. if (msg_len %4)
  208. padding = 4 - msg_len % 4;
  209. msg = smb_message_new(SMB_CMD_TRANS2);
  210. if (!msg) {
  211. free(utf_path);
  212. return (0);
  213. }
  214. msg->packet->header.tid = tid;
  215. SMB_MSG_INIT_PKT(tr2);
  216. tr2.wct = 15;
  217. tr2.total_param_count = utf_path_len + sizeof(smb_tr2_query);
  218. tr2.param_count = tr2.total_param_count;
  219. tr2.max_param_count = 2; // ?? Why not the same or 12 ?
  220. tr2.max_data_count = 0xffff;
  221. tr2.param_offset = 68; // Offset of find_first_params in packet;
  222. tr2.data_count = 0;
  223. tr2.data_offset = 96; // Offset of pattern in packet
  224. tr2.setup_count = 1;
  225. tr2.cmd = SMB_TR2_QUERY_PATH;
  226. tr2.bct = sizeof(smb_tr2_query) + utf_path_len + padding;
  227. SMB_MSG_PUT_PKT(msg, tr2);
  228. SMB_MSG_INIT_PKT(query);
  229. query.interest = 0x0107; // Query File All Info
  230. SMB_MSG_PUT_PKT(msg, query);
  231. smb_message_append(msg, utf_path, utf_path_len);
  232. free(utf_path);
  233. // Adds padding at the end if necessary.
  234. while (padding--)
  235. smb_message_put8(msg, 0);
  236. res = smb_session_send_msg(s, msg);
  237. smb_message_destroy(msg);
  238. if (!res)
  239. {
  240. BDSM_dbg("Unable to query pattern: %s\n", path);
  241. return (NULL);
  242. }
  243. if (!smb_session_recv_msg(s, &reply)
  244. || reply.packet->header.status != NT_STATUS_SUCCESS)
  245. {
  246. BDSM_dbg("Unable to recv msg or failure for %s\n", path);
  247. return (NULL);
  248. }
  249. tr2_resp = (smb_trans2_resp *)reply.packet->payload;
  250. info = (smb_tr2_path_info *)(tr2_resp->payload + 4); //+4 is padding
  251. file = calloc(1, sizeof(smb_file));
  252. if (!file)
  253. return (NULL);
  254. file->name_len = smb_from_utf16((const char *)info->name, info->name_len,
  255. &file->name);
  256. file->name[info->name_len / 2] = 0;
  257. file->created = info->created;
  258. file->accessed = info->accessed;
  259. file->written = info->written;
  260. file->changed = info->changed;
  261. file->alloc_size = info->alloc_size;
  262. file->size = info->size;
  263. file->attr = info->attr;
  264. file->is_dir = info->is_dir;
  265. return (file);
  266. }