dsm.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "config.h"
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <assert.h>
  35. #include <unistd.h>
  36. #include <sys/stat.h>
  37. #include <fcntl.h>
  38. #include <errno.h>
  39. #include <inttypes.h>
  40. #if !defined _WIN32
  41. # include <arpa/inet.h>
  42. # include <netinet/in.h>
  43. #else
  44. # include <winsock2.h>
  45. #endif
  46. #include <getopt.h>
  47. #include "bdsm.h"
  48. #define NBT_UDP_PORT 138
  49. #define NBT_TCP_PORT 139
  50. #define USAGE
  51. /* *INDENT-OFF* */
  52. char usage_str[] = {
  53. "usage: %s [options] host login password share file\n"
  54. " -h, --help Show this help screen.\n"
  55. " -v, --version Print the version and quit.\n"
  56. };
  57. /* *INDENT-ON* */
  58. static void print_usage(const char *pname, int err)
  59. {
  60. fprintf(stderr, usage_str, pname);
  61. exit(err);
  62. }
  63. static int parse_options(int argc, char * argv[])
  64. {
  65. struct option long_options[] = {
  66. {"help", no_argument, 0, 'h'},
  67. {"version", no_argument, 0, 'v'},
  68. {0, 0, 0, 0},
  69. };
  70. int c, opt_index = 0;
  71. char const *pname = ((pname = strrchr(argv[0], '/')) != NULL) ? pname + 1 : argv[0];
  72. while (0 < (c = getopt_long(argc, argv, "hv", long_options, &opt_index)) ) {
  73. switch (c) {
  74. case 'h':
  75. print_usage(pname, 0);
  76. case 'v':
  77. fprintf(stderr, "v%s\n", VERSION);
  78. exit(0);
  79. default:
  80. fprintf(stderr, "unknown option, %c, in getopt_long.\n", c);
  81. exit(-1);
  82. }
  83. }
  84. return optind;
  85. }
  86. int main(int ac, char **av)
  87. {
  88. const char *pname, *host, *login, *password, *fname, *share;
  89. struct sockaddr_in addr;
  90. netbios_ns *ns;
  91. smb_session *session;
  92. int argoffset;
  93. char **share_list;
  94. smb_file *files;
  95. smb_stat st;
  96. pname = ((pname = strrchr(av[0], '/')) != NULL) ? pname + 1 : av[0];
  97. argoffset = parse_options(ac, av);
  98. if (argoffset >= ac || ac - argoffset != 5) {
  99. print_usage(pname, -1);
  100. }
  101. host = av[argoffset++];
  102. login = av[argoffset++];
  103. password = av[argoffset++];
  104. share = av[argoffset++];
  105. fname = av[argoffset++];
  106. ns = netbios_ns_new();
  107. if (netbios_ns_resolve(ns, host, NETBIOS_FILESERVER, &addr.sin_addr.s_addr))
  108. exit(-1);
  109. printf("%s's IP address is : %s\n", host, inet_ntoa(addr.sin_addr));
  110. //netbios_ns_discover(ctx->ns);
  111. //exit(0);
  112. // netbios_session *session;
  113. // session = netbios_session_new(addr.sin_addr.s_addr);
  114. // if (netbios_session_connect(session, "Cerbere"))
  115. // printf("A NetBIOS session with %s has been established\n", host);
  116. // else
  117. // {
  118. // printf("Unable to establish a NetBIOS session with %s\n", host);
  119. // exit(21);
  120. // }
  121. // netbios_session_destroy(session);
  122. session = smb_session_new();
  123. //inet_aton("192.168.110.138", &addr.sin_addr);
  124. if (smb_session_connect(session, host, addr.sin_addr.s_addr, SMB_TRANSPORT_TCP)
  125. == DSM_SUCCESS)
  126. {
  127. printf("Successfully connected to %s\n", host);
  128. }
  129. else
  130. {
  131. printf("Unable to connect to %s\n", host);
  132. exit(42);
  133. }
  134. smb_session_set_creds(session, host, login, password);
  135. if (smb_session_login(session) == DSM_SUCCESS)
  136. {
  137. if (smb_session_is_guest(session))
  138. printf("Login FAILED but we were logged in as GUEST \n");
  139. else
  140. printf("Successfully logged in as %s\\%s\n", host, login);
  141. }
  142. // else if (smb_session_login(session, "WORKGROUP", login, password))
  143. // {
  144. // if (session->guest)
  145. // printf("Login FAILED but we were logged in as GUEST \n");
  146. // else
  147. // printf("Successfully logged in as %s\\%s\n", host, login);
  148. // }
  149. else
  150. {
  151. printf("Authentication FAILURE.\n");
  152. exit(42);
  153. }
  154. if (smb_share_get_list(session, &share_list, NULL) != DSM_SUCCESS)
  155. {
  156. fprintf(stderr, "Unable to list share for %s\n", host);
  157. exit(42);
  158. }
  159. fprintf(stderr, "Share list : \n");
  160. for (size_t j = 0; share_list[j] != NULL; j++)
  161. fprintf(stderr, "- %s\n", share_list[j]);
  162. smb_share_list_destroy(share_list);
  163. smb_tid test;
  164. int ret= smb_tree_connect(session, share, &test);
  165. if (ret == DSM_SUCCESS)
  166. fprintf(stderr, "Connected to %s share\n", share);
  167. else
  168. {
  169. fprintf(stderr, "Unable to connect to %s share: %d\n", share, ret);
  170. exit(42);
  171. }
  172. // smb_fd fd = smb_fopen(session, test, "\\BDSM\\test.txt", SMB_MOD_RO);
  173. // if (fd)
  174. // fprintf(stderr, "Successfully opened file: fd = 0x%.8x\n", fd);
  175. // else
  176. // {
  177. // fprintf(stderr, "Unable to open file\n");
  178. // exit(42);
  179. // }
  180. //char data[1024];
  181. // smb_fread(session, fd, data, 1024);
  182. // fprintf(stderr, "Read from file:\n%s\n", data);
  183. // smb_fclose(session, fd);
  184. fprintf(stderr, "Let's find files at share's root :\n");
  185. files = smb_find(session, test, "\\*");
  186. size_t files_count = smb_stat_list_count( files );
  187. if (files_count <= 0)
  188. fprintf(stderr, "Unable to list files\n");
  189. else
  190. {
  191. for( size_t i = 0; i < files_count; i++ )
  192. {
  193. st = smb_stat_list_at( files, i );
  194. if( st == NULL ) {
  195. fprintf(stderr, "smb_stat_list_at failed\n");
  196. break;
  197. }
  198. fprintf(stdout, "Found a file %s \n", smb_stat_name( st ));
  199. }
  200. }
  201. smb_stat_list_destroy(files);
  202. fprintf(stderr, "Query file info for path: %s\n", fname);
  203. st = smb_fstat(session, test, fname);
  204. if (st != NULL)
  205. {
  206. printf("File '%s' is %"PRIu64" bytes long. is_dir: %"PRIu64"\n", fname,
  207. smb_stat_get(st, SMB_STAT_SIZE), smb_stat_get(st, SMB_STAT_ISDIR));
  208. }
  209. else
  210. {
  211. uint32_t i_status = smb_session_get_nt_status(session);
  212. printf("smb_fstat failed: reason: 0x%X%s\n", i_status,
  213. i_status == NT_STATUS_OBJECT_NAME_NOT_FOUND ? " (file not found)" : "");
  214. }
  215. smb_session_destroy(session);
  216. netbios_ns_destroy(ns);
  217. return 0;
  218. }