dsm.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #define NBT_UDP_PORT 138
  34. #define NBT_TCP_PORT 139
  35. #define USAGE
  36. /* *INDENT-OFF* */
  37. char usage_str[] = {
  38. "usage: %s [options] host login password file\n"
  39. " -h, --help Show this help screen.\n"
  40. " -v, --version Print the version and quit.\n"
  41. };
  42. /* *INDENT-ON* */
  43. static void print_usage(const char *pname, int err)
  44. {
  45. fprintf(stderr, usage_str, pname);
  46. exit(err);
  47. }
  48. static int parse_options(int argc, char * argv[])
  49. {
  50. struct option long_options[] = {
  51. {"help", no_argument, 0, 'h'},
  52. {"version", no_argument, 0, 'v'},
  53. {0, 0, 0, 0},
  54. };
  55. int c, opt_index = 0;
  56. char const *pname = ((pname = strrchr(argv[0], '/')) != NULL) ? pname + 1 : argv[0];
  57. while (0 < (c = getopt_long(argc, argv, "hv", long_options, &opt_index)) ) {
  58. switch (c) {
  59. case 'h':
  60. print_usage(pname, 0);
  61. case 'v':
  62. fprintf(stderr, "v%s\n", VERSION);
  63. exit(0);
  64. default:
  65. fprintf(stderr, "unknown option, %c, in getopt_long.\n", c);
  66. exit(-1);
  67. }
  68. }
  69. return optind;
  70. }
  71. int main(int ac, char **av)
  72. {
  73. const char *pname, *host, *login, *password, *fname;
  74. struct sockaddr_in addr;
  75. netbios_ns *ns;
  76. smb_session *session;
  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. ns = netbios_ns_new();
  88. if (!netbios_ns_resolve(ns, host, NETBIOS_FILESERVER, &addr.sin_addr.s_addr))
  89. exit(-1);
  90. printf("%s's IP address is : %s\n", host, inet_ntoa(addr.sin_addr));
  91. //netbios_ns_discover(ctx->ns);
  92. //exit(0);
  93. // netbios_session *session;
  94. // session = netbios_session_new(addr.sin_addr.s_addr);
  95. // if (netbios_session_connect(session, "Cerbere"))
  96. // printf("A NetBIOS session with %s has been established\n", host);
  97. // else
  98. // {
  99. // printf("Unable to establish a NetBIOS session with %s\n", host);
  100. // exit(21);
  101. // }
  102. // netbios_session_destroy(session);
  103. session = smb_session_new();
  104. if (smb_session_connect(session, host, addr.sin_addr.s_addr))
  105. {
  106. printf("Successfully connected to %s\n", host);
  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 connect to %s\n", host);
  113. exit(42);
  114. }
  115. if (smb_session_login(session, host, login, password))
  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", host, login);
  121. }
  122. else if (smb_session_login(session, "WORKGROUP", login, password))
  123. {
  124. if (session->guest)
  125. printf("Login FAILED but we were logged in as GUEST \n");
  126. else
  127. printf("Successfully logged in as %s\\%s\n", host, login);
  128. }
  129. else
  130. {
  131. printf("Authentication FAILURE.\n");
  132. exit(42);
  133. }
  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. char **share_list;
  152. smb_file *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_get_list(session, &share_list))
  157. {
  158. fprintf(stderr, "Unable to list share for %s\n", host);
  159. exit(42);
  160. }
  161. fprintf(stderr, "Share list : \n");
  162. for (size_t j = 0; share_list[j] != NULL; j++)
  163. fprintf(stderr, "- %s\n", share_list[j]);
  164. smb_share_list_destroy(share_list);
  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(stdout, "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", fname);
  176. files = smb_fstat(session, test, fname);
  177. if (files != NULL)
  178. printf("File '%s' is %lu bytes long\n", fname, files->size);
  179. smb_session_destroy(session);
  180. netbios_ns_destroy(ns);
  181. return (0);
  182. }