dsm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*****************************************************************************
  2. * __________________ _________ _____ _____ .__ ._.
  3. * \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
  4. * | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
  5. * | | \| ` \/ / Y \ / | | \ ___/ \|
  6. * |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
  7. * \/ \/ \/ \/ )/ \/ \/ \/
  8. *
  9. * This file is part of liBDSM. Copyright © 2014-2015 VideoLabs SAS
  10. *
  11. * Author: Julien 'Lta' BALLET <contact@lta.io>
  12. *
  13. * liBDSM is released under LGPLv2.1 (or later) and is also available
  14. * under a commercial license.
  15. *****************************************************************************
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU Lesser General Public License as published by
  18. * the Free Software Foundation; either version 2.1 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Lesser General Public License
  27. * along with this program; if not, write to the Free Software Foundation,
  28. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  29. *****************************************************************************/
  30. #include "config.h"
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <assert.h>
  35. #include <unistd.h>
  36. #include <sys/stat.h>
  37. #include <fcntl.h>
  38. #include <errno.h>
  39. #include <inttypes.h>
  40. #include <sys/socket.h>
  41. #include <netinet/in.h>
  42. #include <netinet/ip.h>
  43. #include <arpa/inet.h>
  44. #include <getopt.h>
  45. #include "bdsm.h"
  46. #define NBT_UDP_PORT 138
  47. #define NBT_TCP_PORT 139
  48. #define USAGE
  49. /* *INDENT-OFF* */
  50. char usage_str[] = {
  51. "usage: %s [options] host login password share file\n"
  52. " -h, --help Show this help screen.\n"
  53. " -v, --version Print the version and quit.\n"
  54. };
  55. /* *INDENT-ON* */
  56. static void print_usage(const char *pname, int err)
  57. {
  58. fprintf(stderr, usage_str, pname);
  59. exit(err);
  60. }
  61. static int parse_options(int argc, char * argv[])
  62. {
  63. struct option long_options[] = {
  64. {"help", no_argument, 0, 'h'},
  65. {"version", no_argument, 0, 'v'},
  66. {0, 0, 0, 0},
  67. };
  68. int c, opt_index = 0;
  69. char const *pname = ((pname = strrchr(argv[0], '/')) != NULL) ? pname + 1 : argv[0];
  70. while (0 < (c = getopt_long(argc, argv, "hv", long_options, &opt_index)) ) {
  71. switch (c) {
  72. case 'h':
  73. print_usage(pname, 0);
  74. case 'v':
  75. fprintf(stderr, "v%s\n", VERSION);
  76. exit(0);
  77. default:
  78. fprintf(stderr, "unknown option, %c, in getopt_long.\n", c);
  79. exit(-1);
  80. }
  81. }
  82. return optind;
  83. }
  84. int main(int ac, char **av)
  85. {
  86. const char *pname, *host, *login, *password, *fname, *share;
  87. struct sockaddr_in addr;
  88. netbios_ns *ns;
  89. smb_session *session;
  90. int argoffset;
  91. char **share_list;
  92. smb_file *files;
  93. smb_stat st;
  94. pname = ((pname = strrchr(av[0], '/')) != NULL) ? pname + 1 : av[0];
  95. argoffset = parse_options(ac, av);
  96. if (argoffset >= ac || ac - argoffset != 5) {
  97. print_usage(pname, -1);
  98. }
  99. host = av[argoffset++];
  100. login = av[argoffset++];
  101. password = av[argoffset++];
  102. share = av[argoffset++];
  103. fname = av[argoffset++];
  104. ns = netbios_ns_new();
  105. if (!netbios_ns_resolve(ns, host, NETBIOS_FILESERVER, &addr.sin_addr.s_addr))
  106. exit(-1);
  107. printf("%s's IP address is : %s\n", host, inet_ntoa(addr.sin_addr));
  108. //netbios_ns_discover(ctx->ns);
  109. //exit(0);
  110. // netbios_session *session;
  111. // session = netbios_session_new(addr.sin_addr.s_addr);
  112. // if (netbios_session_connect(session, "Cerbere"))
  113. // printf("A NetBIOS session with %s has been established\n", host);
  114. // else
  115. // {
  116. // printf("Unable to establish a NetBIOS session with %s\n", host);
  117. // exit(21);
  118. // }
  119. // netbios_session_destroy(session);
  120. session = smb_session_new();
  121. //inet_aton("192.168.110.138", &addr.sin_addr);
  122. if (smb_session_connect(session, host, addr.sin_addr.s_addr, SMB_TRANSPORT_TCP))
  123. {
  124. printf("Successfully connected to %s\n", host);
  125. }
  126. else
  127. {
  128. printf("Unable to connect to %s\n", host);
  129. exit(42);
  130. }
  131. smb_session_set_creds(session, host, login, password);
  132. if (smb_session_login(session))
  133. {
  134. if (smb_session_is_guest(session))
  135. printf("Login FAILED but we were logged in as GUEST \n");
  136. else
  137. printf("Successfully logged in as %s\\%s\n", host, login);
  138. }
  139. // else if (smb_session_login(session, "WORKGROUP", login, password))
  140. // {
  141. // if (session->guest)
  142. // printf("Login FAILED but we were logged in as GUEST \n");
  143. // else
  144. // printf("Successfully logged in as %s\\%s\n", host, login);
  145. // }
  146. else
  147. {
  148. printf("Authentication FAILURE.\n");
  149. exit(42);
  150. }
  151. if (!smb_share_get_list(session, &share_list))
  152. {
  153. fprintf(stderr, "Unable to list share for %s\n", host);
  154. exit(42);
  155. }
  156. fprintf(stderr, "Share list : \n");
  157. for (size_t j = 0; share_list[j] != NULL; j++)
  158. fprintf(stderr, "- %s\n", share_list[j]);
  159. smb_share_list_destroy(share_list);
  160. smb_tid test = smb_tree_connect(session, share);
  161. if (test != -1)
  162. fprintf(stderr, "Connected to %s share\n", share);
  163. else
  164. {
  165. fprintf(stderr, "Unable to connect to %s share\n", share);
  166. exit(42);
  167. }
  168. // smb_fd fd = smb_fopen(session, test, "\\BDSM\\test.txt", SMB_MOD_RO);
  169. // if (fd)
  170. // fprintf(stderr, "Successfully opened file: fd = 0x%.8x\n", fd);
  171. // else
  172. // {
  173. // fprintf(stderr, "Unable to open file\n");
  174. // exit(42);
  175. // }
  176. //char data[1024];
  177. // smb_fread(session, fd, data, 1024);
  178. // fprintf(stderr, "Read from file:\n%s\n", data);
  179. // smb_fclose(session, fd);
  180. fprintf(stderr, "Let's find files at share's root :\n");
  181. files = smb_find(session, test, "\\*");
  182. size_t files_count = smb_stat_list_count( files );
  183. if (files_count <= 0)
  184. fprintf(stderr, "Unable to list files\n");
  185. else
  186. {
  187. for( size_t i = 0; i < files_count; i++ )
  188. {
  189. st = smb_stat_list_at( files, i );
  190. if( st == NULL ) {
  191. fprintf(stderr, "smb_stat_list_at failed\n");
  192. break;
  193. }
  194. fprintf(stdout, "Found a file %s \n", smb_stat_name( st ));
  195. }
  196. }
  197. smb_stat_list_destroy(files);
  198. fprintf(stderr, "Query file info for path: %s\n", fname);
  199. st = smb_fstat(session, test, fname);
  200. if (st != NULL)
  201. {
  202. printf("File '%s' is %"PRIu64" bytes long\n", fname, smb_stat_get(st, SMB_STAT_SIZE));
  203. }
  204. smb_session_destroy(session);
  205. netbios_ns_destroy(ns);
  206. return 0;
  207. }