dsm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. struct sockaddr_in addr;
  69. bdsm_context_t *ctx;
  70. ctx = bdsm_context_new();
  71. assert(ctx);
  72. addr.sin_addr.s_addr = netbios_ns_resolve(ctx->ns, av[1], NETBIOS_FILESERVER);
  73. printf("%s's IP address is : %s\n", av[1], inet_ntoa(addr.sin_addr));
  74. //netbios_ns_discover(ctx->ns);
  75. //exit(0);
  76. // netbios_session_t *session;
  77. // session = netbios_session_new(addr.sin_addr.s_addr);
  78. // if (netbios_session_connect(session, "Cerbere"))
  79. // printf("A NetBIOS session with %s has been established\n", av[1]);
  80. // else
  81. // {
  82. // printf("Unable to establish a NetBIOS session with %s\n", av[1]);
  83. // exit(21);
  84. // }
  85. // netbios_session_destroy(session);
  86. smb_session_t *session;
  87. session = smb_session_new();
  88. if (smb_session_connect(session, av[1], addr.sin_addr.s_addr))
  89. printf("Successfully connected to %s\n", av[1]);
  90. else
  91. {
  92. printf("Unable to connect to %s\n", av[1]);
  93. exit(42);
  94. }
  95. if (smb_negotiate(session))
  96. {
  97. fprintf(stderr, "Dialect/Security Mode negotation success.\n");
  98. fprintf(stderr, "Session key is 0x%x\n", session->srv.session_key);
  99. fprintf(stderr, "Challenge key is 0x%lx\n", session->srv.challenge);
  100. }
  101. else
  102. {
  103. printf("Unable to negotiate SMB Dialect\n");
  104. exit(42);
  105. }
  106. if (smb_authenticate(session, av[1], av[2], av[3]))
  107. {
  108. if (session->guest)
  109. printf("Login FAILED but we were logged in as GUEST \n");
  110. else
  111. printf("Successfully logged in as %s\\%s\n", av[1], av[2]);
  112. }
  113. else
  114. {
  115. printf("Authentication FAILURE.\n");
  116. exit(42);
  117. }
  118. smb_tid ipc = smb_tree_connect(session, "IPC$");
  119. if (ipc == 0)
  120. {
  121. fprintf(stderr, "Unable to connect to IPC$ share\n");
  122. exit(42);
  123. }
  124. fprintf(stderr, "Connected to IPC$ share\n");
  125. smb_tid test = smb_tree_connect(session, "test");
  126. if (test)
  127. fprintf(stderr, "Connected to Test share\n");
  128. else
  129. {
  130. fprintf(stderr, "Unable to connect to Test share\n");
  131. exit(42);
  132. }
  133. // smb_fd fd = smb_fopen(session, test, "\\BDSM\\test.txt", SMB_MOD_RO);
  134. // if (fd)
  135. // fprintf(stderr, "Successfully opened file: fd = 0x%.8x\n", fd);
  136. // else
  137. // {
  138. // fprintf(stderr, "Unable to open file\n");
  139. // exit(42);
  140. // }
  141. char data[1024];
  142. smb_share_list_t *share_list;
  143. smb_file_t *files;
  144. // smb_fread(session, fd, data, 1024);
  145. // fprintf(stderr, "Read from file:\n%s\n", data);
  146. // smb_fclose(session, fd);
  147. if (!smb_share_list(session, &share_list))
  148. {
  149. fprintf(stderr, "Unable to list share for %s\n", av[1]);
  150. exit(42);
  151. }
  152. else
  153. {
  154. fprintf(stderr, "Share list : \n");
  155. }
  156. fprintf(stderr, "Let's find files at share's root :\n");
  157. files = smb_find(session, test, "\\*");
  158. if (files != NULL)
  159. while(files)
  160. {
  161. fprintf(stderr, "Found a file %s \n", files->name);
  162. files = files->next;
  163. }
  164. else
  165. fprintf(stderr, "Unable to list files\n");
  166. fprintf(stderr, "Query file info for path: %s\n", av[4]);
  167. files = smb_stat(session, test, av[4]);
  168. if (files)
  169. printf("File %s is %lu bytes long\n", av[4], files->size);
  170. smb_session_destroy(session);
  171. bdsm_context_destroy(ctx);
  172. return (0);
  173. }