dsm.c 5.7 KB

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