smb_dir.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*****************************************************************************
  2. * __________________ _________ _____ _____ .__ ._.
  3. * \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
  4. * | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
  5. * | | \| ` \/ / Y \ / | | \ ___/ \|
  6. * |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
  7. * \/ \/ \/ \/ )/ \/ \/ \/
  8. *
  9. * This file is part of liBDSM. Copyright © 2014-2015 VideoLabs SAS
  10. *
  11. * Author: Sylver Bruneau <sylver.bruneau@gmail.com>
  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. #ifdef HAVE_CONFIG_H
  31. # include "config.h"
  32. #endif
  33. #include <assert.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. #include <string.h>
  37. #include <stdio.h>
  38. #include "smb_session_msg.h"
  39. #include "smb_fd.h"
  40. #include "smb_utils.h"
  41. #include "smb_dir.h"
  42. int smb_directory_rm(smb_session *s, smb_tid tid, const char *path)
  43. {
  44. smb_message *req_msg, resp_msg;
  45. smb_directory_rm_req req;
  46. smb_directory_rm_resp *resp;
  47. size_t utf_pattern_len;
  48. char *utf_pattern;
  49. assert(s != NULL && path != NULL);
  50. utf_pattern_len = smb_to_utf16(path, strlen(path) + 1, &utf_pattern);
  51. if (utf_pattern_len == 0)
  52. return DSM_ERROR_CHARSET;
  53. req_msg = smb_message_new(SMB_CMD_RMDIR);
  54. if (!req_msg)
  55. {
  56. free(utf_pattern);
  57. return DSM_ERROR_GENERIC;
  58. }
  59. req_msg->packet->header.tid = (uint16_t)tid;
  60. SMB_MSG_INIT_PKT(req);
  61. req.wct = 0x00; // Must be 0
  62. req.bct = (uint16_t)(utf_pattern_len + 1);
  63. req.buffer_format = 0x04; // Must be 4
  64. SMB_MSG_PUT_PKT(req_msg, req);
  65. smb_message_append(req_msg, utf_pattern, utf_pattern_len);
  66. smb_session_send_msg(s, req_msg);
  67. smb_message_destroy(req_msg);
  68. free(utf_pattern);
  69. if (!smb_session_recv_msg(s, &resp_msg))
  70. return DSM_ERROR_NETWORK;
  71. if (!smb_session_check_nt_status(s, &resp_msg))
  72. return DSM_ERROR_NT;
  73. resp = (smb_directory_rm_resp *)resp_msg.packet->payload;
  74. if ((resp->wct != 0) || (resp->bct != 0))
  75. return DSM_ERROR_NETWORK;
  76. return 0;
  77. }
  78. int smb_directory_create(smb_session *s, smb_tid tid, const char *path)
  79. {
  80. smb_message *req_msg, resp_msg;
  81. smb_directory_mk_req req;
  82. smb_directory_mk_resp *resp;
  83. size_t utf_pattern_len;
  84. char *utf_pattern;
  85. assert(s != NULL && path != NULL);
  86. utf_pattern_len = smb_to_utf16(path, strlen(path) + 1, &utf_pattern);
  87. if (utf_pattern_len == 0)
  88. return DSM_ERROR_CHARSET;
  89. req_msg = smb_message_new(SMB_CMD_MKDIR);
  90. if (!req_msg)
  91. {
  92. free(utf_pattern);
  93. return DSM_ERROR_GENERIC;
  94. }
  95. req_msg->packet->header.tid = (uint16_t)tid;
  96. SMB_MSG_INIT_PKT(req);
  97. req.wct = 0x00; // Must be 0
  98. req.bct = (uint16_t)(utf_pattern_len + 1);
  99. req.buffer_format = 0x04; // Must be 4
  100. SMB_MSG_PUT_PKT(req_msg, req);
  101. smb_message_append(req_msg, utf_pattern, utf_pattern_len);
  102. smb_session_send_msg(s, req_msg);
  103. smb_message_destroy(req_msg);
  104. free(utf_pattern);
  105. if (!smb_session_recv_msg(s, &resp_msg))
  106. return DSM_ERROR_NETWORK;
  107. if (!smb_session_check_nt_status(s, &resp_msg))
  108. return DSM_ERROR_NT;
  109. resp = (smb_directory_mk_resp *)resp_msg.packet->payload;
  110. if ((resp->wct != 0) || (resp->bct != 0))
  111. return DSM_ERROR_NETWORK;
  112. return 0;
  113. }