smb_session.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifndef __BDSM_SMB_SESSION_H_
  19. #define __BDSM_SMB_SESSION_H_
  20. #include "bdsm/netbios_session.h"
  21. #include "bdsm/smb_defs.h"
  22. #include "bdsm/smb_message.h"
  23. #define SMB_STATE_SESSION_OK 3 // We are authenticated and can do stuff
  24. #define SMB_STATE_DIALECT_OK 2
  25. #define SMB_STATE_NETBIOS_OK 1
  26. #define SMB_STATE_NEW 0
  27. #define SMB_STATE_ERROR -1
  28. // The id of a connection to a share, i.e. 'TreeID'
  29. typedef uint16_t smb_tid;
  30. // The id of a file handle, i.e. 'FUID'
  31. typedef uint16_t smb_fid;
  32. // Concatenation of the two above, representing a file inside of a session
  33. // First 4 bytes are the TreeID (smb_tid), last 4 are the File ID (FUID)
  34. typedef uint32_t smb_fd;
  35. #define SMB_FD_TID(fd) ((smb_tid)(fd >> 16))
  36. #define SMB_FD_FID(fd) ((smb_fid)(fd & 0x0000ffff))
  37. #define SMB_FD(tid, fid) ((((smb_fd)tid) << 16) | (((smb_fd) fid)))
  38. typedef struct smb_file_s
  39. {
  40. smb_fid fid;
  41. struct smb_file_s *next; // Next file in this share
  42. smb_tid tid;
  43. uint64_t created;
  44. uint64_t accessed;
  45. uint64_t written;
  46. uint64_t changed;
  47. uint64_t alloc_size;
  48. uint64_t size;
  49. uint32_t attr;
  50. uint32_t readp; // Current read pointer (position);
  51. int is_dir; // 0 -> file, 1 -> directory
  52. } smb_file_t;
  53. typedef struct smb_share_s
  54. {
  55. smb_tid tid;
  56. struct smb_share_s *next; // Next share in this session
  57. uint16_t opts; // Optionnal support opts
  58. uint16_t rights; // Maximum rights field
  59. uint16_t guest_rights;
  60. struct smb_file_s *files; // List of all open files for this share
  61. } smb_share_t;
  62. typedef struct
  63. {
  64. int state;
  65. int guest; // boolean, are we logged as guest ?
  66. uint16_t uid; // uid attributed by the server
  67. netbios_session_t *nb_session;
  68. // Informations about the smb server we are connected to.
  69. struct {
  70. char name[16]; // The server name
  71. uint16_t dialect; // The selected dialect
  72. uint16_t security_mode; // Security mode
  73. uint32_t caps; // Server caps replyed during negotiate
  74. uint32_t session_key; // XXX Is this really usefull?
  75. uint64_t challenge; // For challenge response security
  76. uint64_t ts; // It seems Win7 requires it :-/
  77. } srv;
  78. struct smb_share_s *shares; // shares->files | Map fd <-> smb_file_t
  79. } smb_session_t;
  80. smb_session_t *smb_session_new();
  81. void smb_session_destroy(smb_session_t *s);
  82. void smb_session_share_add(smb_session_t *s, smb_share_t *share);
  83. smb_share_t *smb_session_share_get(smb_session_t *s, smb_tid tid);
  84. smb_share_t *smb_session_share_remove(smb_session_t *s, smb_tid tid);
  85. int smb_session_file_add(smb_session_t *s, smb_tid tid,
  86. smb_file_t *f);
  87. smb_file_t *smb_session_file_get(smb_session_t *s, smb_fd fd);
  88. smb_file_t *smb_session_file_remove(smb_session_t *s, smb_fd fd);
  89. int smb_session_send_msg(smb_session_t *s, smb_message_t *msg);
  90. // msg->packet will be updated to point on received data. You don't own this
  91. // memory. It'll be reused on next recv_msg
  92. size_t smb_session_recv_msg(smb_session_t *s, smb_message_t *msg);
  93. int smb_session_connect(smb_session_t *s, char *name, uint32_t ip);
  94. int smb_negotiate(smb_session_t *s);
  95. int smb_authenticate(smb_session_t *s, const char *domain,
  96. const char *user, const char *password);
  97. #endif