dsm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //---------------------------------------------------------------------------
  2. // __________________ _________ _____ _____ .__ ._.
  3. // \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
  4. // | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
  5. // | | \| ` \/ / Y \ / | | \ ___/ \|
  6. // |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
  7. // \/ \/ \/ \/ )/ \/ \/ \/
  8. //
  9. // This file is part of libdsm. Copyright © 2014 VideoLabs SAS
  10. //
  11. // Author: Julien 'Lta' BALLET <contact@lta.io>
  12. //
  13. // This program is free software. It comes without any warranty, to the extent
  14. // permitted by applicable law. You can redistribute it and/or modify it under
  15. // the terms of the Do What The Fuck You Want To Public License, Version 2, as
  16. // published by Sam Hocevar. See the COPYING file for more details.
  17. //----------------------------------------------------------------------------
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include <unistd.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <netinet/ip.h>
  29. #include <netinet/udp.h>
  30. #include <arpa/inet.h>
  31. #define NBT_UDP_PORT 138
  32. #define NBT_TCP_PORT 139
  33. #include "bdsm.h"
  34. #include "bdsm/smb_trans2.h"
  35. #include <getopt.h>
  36. static int parse_options(int argc, char * argv[])
  37. {
  38. /* *INDENT-OFF* */
  39. char usage_str[] = {
  40. " -h, --help Show this help screen.\n"
  41. " -v, --version Print the version and quit.\n"
  42. };
  43. /* *INDENT-ON* */
  44. struct option long_options[] = {
  45. {"help", no_argument, 0, 'h'},
  46. {"version", no_argument, 0, 'v'},
  47. {0, 0, 0, 0},
  48. };
  49. int c, opt_index = 0;
  50. char const *pname = ((pname = strrchr(argv[0], '/')) != NULL) ? pname + 1 : argv[0];
  51. while (0 < (c = getopt_long(argc, argv, "hv", long_options, &opt_index)) ) {
  52. switch (c) {
  53. case 'h':
  54. fprintf(stderr, "%s", usage_str);
  55. exit(0);
  56. case 'v':
  57. fprintf(stderr, "v%s\n", VERSION);
  58. exit(0);
  59. default:
  60. fprintf(stderr, "unknown option, %c, in getopt_long.\n", c);
  61. exit(-1);
  62. }
  63. }
  64. return optind;
  65. }
  66. int main(int ac, char **av)
  67. {
  68. char const *pname = ((pname = strrchr(av[0], '/')) != NULL) ? pname + 1 : av[0];
  69. int const argoffset = parse_options(ac, av);
  70. if (argoffset >= ac) {
  71. fprintf(stderr, "usage: %s [options] host\n", pname);
  72. exit(-1);
  73. }
  74. char * hoststr = av[argoffset];
  75. struct sockaddr_in addr;
  76. bdsm_context_t *ctx;
  77. ctx = bdsm_context_new();
  78. assert(ctx);
  79. if (0 != netbios_ns_resolve(ctx->ns, hoststr, NETBIOS_FILESERVER, &addr.sin_addr.s_addr)) {
  80. exit(-1);
  81. }
  82. printf("%s's IP address is : %s\n", hoststr, inet_ntoa(addr.sin_addr));
  83. //netbios_ns_discover(ctx->ns);
  84. //exit(0);
  85. // netbios_session_t *session;
  86. // session = netbios_session_new(addr.sin_addr.s_addr);
  87. // if (netbios_session_connect(session, "Cerbere"))
  88. // printf("A NetBIOS session with %s has been established\n", hoststr);
  89. // else
  90. // {
  91. // printf("Unable to establish a NetBIOS session with %s\n", hoststr);
  92. // exit(21);
  93. // }
  94. // netbios_session_destroy(session);
  95. smb_session_t *session;
  96. session = smb_session_new();
  97. if (smb_session_connect(session, hoststr, addr.sin_addr.s_addr))
  98. printf("Successfully connected to %s\n", hoststr);
  99. else
  100. {
  101. printf("Unable to connect to %s\n", hoststr);
  102. exit(42);
  103. }
  104. if (smb_negotiate(session))
  105. {
  106. fprintf(stderr, "Dialect/Security Mode negotation success.\n");
  107. fprintf(stderr, "Session key is 0x%x\n", session->srv.session_key);
  108. fprintf(stderr, "Challenge key is 0x%lx\n", session->srv.challenge);
  109. }
  110. else
  111. {
  112. printf("Unable to negotiate SMB Dialect\n");
  113. exit(42);
  114. }
  115. if (smb_authenticate(session, hoststr, av[2], av[3]))
  116. {
  117. if (session->guest)
  118. printf("Login FAILED but we were logged in as GUEST \n");
  119. else
  120. printf("Successfully logged in as %s\\%s\n", hoststr, av[2]);
  121. }
  122. else
  123. {
  124. printf("Authentication FAILURE.\n");
  125. exit(42);
  126. }
  127. smb_tid ipc = smb_tree_connect(session, "IPC$");
  128. if (ipc == 0)
  129. {
  130. fprintf(stderr, "Unable to connect to IPC$ share\n");
  131. exit(42);
  132. }
  133. fprintf(stderr, "Connected to IPC$ share\n");
  134. smb_tid test = smb_tree_connect(session, "test");
  135. if (test)
  136. fprintf(stderr, "Connected to Test share\n");
  137. else
  138. {
  139. fprintf(stderr, "Unable to connect to Test share\n");
  140. exit(42);
  141. }
  142. // smb_fd fd = smb_fopen(session, test, "\\BDSM\\test.txt", SMB_MOD_RO);
  143. // if (fd)
  144. // fprintf(stderr, "Successfully opened file: fd = 0x%.8x\n", fd);
  145. // else
  146. // {
  147. // fprintf(stderr, "Unable to open file\n");
  148. // exit(42);
  149. // }
  150. char data[1024];
  151. smb_share_list_t *share_list;
  152. smb_file_t *files;
  153. // smb_fread(session, fd, data, 1024);
  154. // fprintf(stderr, "Read from file:\n%s\n", data);
  155. // smb_fclose(session, fd);
  156. if (!smb_share_list(session, &share_list))
  157. {
  158. fprintf(stderr, "Unable to list share for %s\n", hoststr);
  159. exit(42);
  160. }
  161. else
  162. {
  163. fprintf(stderr, "Share list : \n");
  164. }
  165. fprintf(stderr, "Let's find files at share's root :\n");
  166. files = smb_find(session, test, "\\*");
  167. if (files != NULL)
  168. while(files)
  169. {
  170. fprintf(stderr, "Found a file %s \n", files->name);
  171. files = files->next;
  172. }
  173. else
  174. fprintf(stderr, "Unable to list files\n");
  175. fprintf(stderr, "Query file info for path: %s\n", av[4]);
  176. files = smb_stat(session, test, av[4]);
  177. if (files)
  178. printf("File %s is %lu bytes long\n", av[4], files->size);
  179. smb_session_destroy(session);
  180. bdsm_context_destroy(ctx);
  181. return (0);
  182. }