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. struct smb_file_s *next; // Next file in this share
  41. char *name;
  42. smb_fid fid;
  43. smb_tid tid;
  44. size_t name_len;
  45. uint64_t created;
  46. uint64_t accessed;
  47. uint64_t written;
  48. uint64_t changed;
  49. uint64_t alloc_size;
  50. uint64_t size;
  51. uint32_t attr;
  52. uint32_t readp; // Current read pointer (position);
  53. int is_dir; // 0 -> file, 1 -> directory
  54. } smb_file_t;
  55. typedef struct smb_share_s
  56. {
  57. struct smb_share_s *next; // Next share in this session
  58. struct smb_file_s *files; // List of all open files for this share
  59. smb_tid tid;
  60. uint16_t opts; // Optionnal support opts
  61. uint16_t rights; // Maximum rights field
  62. uint16_t guest_rights;
  63. } smb_share_t;
  64. typedef struct
  65. {
  66. int state;
  67. int guest; // boolean, are we logged as guest ?
  68. uint16_t uid; // uid attributed by the server
  69. netbios_session_t *nb_session;
  70. // Informations about the smb server we are connected to.
  71. struct {
  72. char name[16]; // The server name
  73. uint16_t dialect; // The selected dialect
  74. uint16_t security_mode; // Security mode
  75. uint32_t caps; // Server caps replyed during negotiate
  76. uint32_t session_key; // XXX Is this really usefull?
  77. uint64_t challenge; // For challenge response security
  78. uint64_t ts; // It seems Win7 requires it :-/
  79. } srv;
  80. struct smb_share_s *shares; // shares->files | Map fd <-> smb_file_t
  81. } smb_session_t;
  82. smb_session_t *smb_session_new();
  83. void smb_session_destroy(smb_session_t *s);
  84. void smb_session_share_add(smb_session_t *s, smb_share_t *share);
  85. smb_share_t *smb_session_share_get(smb_session_t *s, smb_tid tid);
  86. smb_share_t *smb_session_share_remove(smb_session_t *s, smb_tid tid);
  87. int smb_session_file_add(smb_session_t *s, smb_tid tid,
  88. smb_file_t *f);
  89. smb_file_t *smb_session_file_get(smb_session_t *s, smb_fd fd);
  90. smb_file_t *smb_session_file_remove(smb_session_t *s, smb_fd fd);
  91. int smb_session_send_msg(smb_session_t *s, smb_message_t *msg);
  92. // msg->packet will be updated to point on received data. You don't own this
  93. // memory. It'll be reused on next recv_msg
  94. size_t smb_session_recv_msg(smb_session_t *s, smb_message_t *msg);
  95. int smb_session_connect(smb_session_t *s, char *name, uint32_t ip);
  96. int smb_negotiate(smb_session_t *s);
  97. int smb_authenticate(smb_session_t *s, const char *domain,
  98. const char *user, const char *password);
  99. #endif