dsm.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #include <getopt.h>
  32. #include "bdsm.h"
  33. #include "bdsm/smb_trans2.h"
  34. #define NBT_UDP_PORT 138
  35. #define NBT_TCP_PORT 139
  36. #define USAGE
  37. /* *INDENT-OFF* */
  38. char usage_str[] = {
  39. "usage: %s [options] host login password file\n"
  40. " -h, --help Show this help screen.\n"
  41. " -v, --version Print the version and quit.\n"
  42. };
  43. /* *INDENT-ON* */
  44. static void print_usage(const char *pname, int err)
  45. {
  46. fprintf(stderr, usage_str, pname);
  47. exit(err);
  48. }
  49. static int parse_options(int argc, char * argv[])
  50. {
  51. struct option long_options[] = {
  52. {"help", no_argument, 0, 'h'},
  53. {"version", no_argument, 0, 'v'},
  54. {0, 0, 0, 0},
  55. };
  56. int c, opt_index = 0;
  57. char const *pname = ((pname = strrchr(argv[0], '/')) != NULL) ? pname + 1 : argv[0];
  58. while (0 < (c = getopt_long(argc, argv, "hv", long_options, &opt_index)) ) {
  59. switch (c) {
  60. case 'h':
  61. print_usage(pname, 0);
  62. case 'v':
  63. fprintf(stderr, "v%s\n", VERSION);
  64. exit(0);
  65. default:
  66. fprintf(stderr, "unknown option, %c, in getopt_long.\n", c);
  67. exit(-1);
  68. }
  69. }
  70. return optind;
  71. }
  72. int main(int ac, char **av)
  73. {
  74. const char *pname, *host, *login, *password, *fname;
  75. struct sockaddr_in addr;
  76. bdsm_context_t *ctx;
  77. int argoffset;
  78. pname = ((pname = strrchr(av[0], '/')) != NULL) ? pname + 1 : av[0];
  79. argoffset = parse_options(ac, av);
  80. if (argoffset >= ac || ac - argoffset != 4) {
  81. print_usage(pname, -1);
  82. }
  83. host = av[argoffset++];
  84. login = av[argoffset++];
  85. password = av[argoffset++];
  86. fname = av[argoffset++];
  87. ctx = bdsm_context_new();
  88. assert(ctx);
  89. if (!netbios_ns_resolve(ctx->ns, host, NETBIOS_FILESERVER, &addr.sin_addr.s_addr))
  90. exit(-1);
  91. printf("%s's IP address is : %s\n", host, inet_ntoa(addr.sin_addr));
  92. //netbios_ns_discover(ctx->ns);
  93. //exit(0);
  94. // netbios_session_t *session;
  95. // session = netbios_session_new(addr.sin_addr.s_addr);
  96. // if (netbios_session_connect(session, "Cerbere"))
  97. // printf("A NetBIOS session with %s has been established\n", host);
  98. // else
  99. // {
  100. // printf("Unable to establish a NetBIOS session with %s\n", host);
  101. // exit(21);
  102. // }
  103. // netbios_session_destroy(session);
  104. smb_session_t *session;
  105. session = smb_session_new();
  106. if (smb_session_connect(session, host, addr.sin_addr.s_addr))
  107. {
  108. printf("Successfully connected to %s\n", host);
  109. fprintf(stderr, "Session key is 0x%x\n", session->srv.session_key);
  110. fprintf(stderr, "Challenge key is 0x%lx\n", session->srv.challenge);
  111. }
  112. else
  113. {
  114. printf("Unable to connect to %s\n", host);
  115. exit(42);
  116. }
  117. if (smb_session_login(session, host, login, password))
  118. {
  119. if (session->guest)
  120. printf("Login FAILED but we were logged in as GUEST \n");
  121. else
  122. printf("Successfully logged in as %s\\%s\n", host, login);
  123. }
  124. else
  125. {
  126. printf("Authentication FAILURE.\n");
  127. exit(42);
  128. }
  129. smb_tid ipc = smb_tree_connect(session, "IPC$");
  130. if (ipc == 0)
  131. {
  132. fprintf(stderr, "Unable to connect to IPC$ share\n");
  133. exit(42);
  134. }
  135. fprintf(stderr, "Connected to IPC$ share\n");
  136. smb_tid test = smb_tree_connect(session, "test");
  137. if (test)
  138. fprintf(stderr, "Connected to Test share\n");
  139. else
  140. {
  141. fprintf(stderr, "Unable to connect to Test share\n");
  142. exit(42);
  143. }
  144. // smb_fd fd = smb_fopen(session, test, "\\BDSM\\test.txt", SMB_MOD_RO);
  145. // if (fd)
  146. // fprintf(stderr, "Successfully opened file: fd = 0x%.8x\n", fd);
  147. // else
  148. // {
  149. // fprintf(stderr, "Unable to open file\n");
  150. // exit(42);
  151. // }
  152. char data[1024];
  153. char **share_list;
  154. smb_file_t *files;
  155. // smb_fread(session, fd, data, 1024);
  156. // fprintf(stderr, "Read from file:\n%s\n", data);
  157. // smb_fclose(session, fd);
  158. if (!smb_share_list(session, &share_list))
  159. {
  160. fprintf(stderr, "Unable to list share for %s\n", host);
  161. exit(42);
  162. }
  163. else
  164. {
  165. fprintf(stderr, "Share list : \n");
  166. for (size_t j; share_list[j] != NULL; j++)
  167. fprintf(stderr, "- %s\n", share_list[j]);
  168. }
  169. fprintf(stderr, "Let's find files at share's root :\n");
  170. files = smb_find(session, test, "\\*");
  171. if (files != NULL)
  172. while(files)
  173. {
  174. fprintf(stderr, "Found a file %s \n", files->name);
  175. files = files->next;
  176. }
  177. else
  178. fprintf(stderr, "Unable to list files\n");
  179. fprintf(stderr, "Query file info for path: %s\n", fname);
  180. files = smb_stat(session, test, fname);
  181. if (files)
  182. printf("File %s is %lu bytes long\n", fname, files->size);
  183. smb_session_destroy(session);
  184. bdsm_context_destroy(ctx);
  185. return (0);
  186. }