smb_session.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_types.h"
  23. #include "bdsm/smb_message.h"
  24. /**
  25. * @file smb_session.h
  26. * @brief Functions to connect and authenticate to an SMB server
  27. */
  28. /**
  29. * @internal
  30. * @brief Get a smb_tid from a smb_fd
  31. *
  32. * @param fd a smb_fd
  33. * @return A smb_tid
  34. */
  35. #define SMB_FD_TID(fd) ((smb_tid)(fd >> 16))
  36. /**
  37. * @internal
  38. * @brief Get a smb_fid from a smb_fd
  39. *
  40. * @param fd a smb_fid
  41. * @return A smb_fid
  42. */
  43. #define SMB_FD_FID(fd) ((smb_fid)(fd & 0x0000ffff))
  44. /**
  45. * @internal
  46. * @brief Compute the smb_fd for the given smb_tid and smb_fid
  47. * @param tid a smb_tid
  48. * @param fid a smb_fid *
  49. * @return A smb_fd
  50. */
  51. #define SMB_FD(tid, fid) ((((smb_fd)tid) << 16) | (((smb_fd) fid)))
  52. /**
  53. * @brief Allocates a new Session object
  54. * @details To be able to perform actions on shares and file, you'll need to
  55. * call smb_session_connect, then authenticate with smb_authenticate.
  56. * @return A new Session object.
  57. */
  58. smb_session *smb_session_new();
  59. /**
  60. * @brief Close a session and deallocate its ressources
  61. * @details After destroying a session, all the smb_tid, smb_fid and smb_fd
  62. * associated with this session becomes invalid.
  63. *
  64. * @param s The session to destroy
  65. */
  66. void smb_session_destroy(smb_session *s);
  67. /**
  68. * @brief Returns the current state of the session
  69. * @see SMB_STATE_ERROR
  70. * @see SMB_STATE_NEW
  71. * @see SMB_STATE_SESSION_OK
  72. */
  73. int smb_session_state(smb_session *s);
  74. // ---------------------------------
  75. // Internal stuff, maybe move this somewhere else
  76. int smb_negotiate(smb_session *s);
  77. int smb_session_send_msg(smb_session *s, smb_message *msg);
  78. // msg->packet will be updated to point on received data. You don't own this
  79. // memory. It'll be reused on next recv_msg
  80. size_t smb_session_recv_msg(smb_session *s, smb_message *msg);
  81. // --------------------------------
  82. /**
  83. * @brief Establish a connection and negotiate a session protocol with a remote
  84. * host
  85. * @details You have to provide both the ip and the name. This is a constraint
  86. * of Netbios, which requires you to know its name before he accepts to speak
  87. * with you.
  88. *
  89. * @param s A session object.
  90. * @param name The ASCII netbios name, the name type will be coerced to <20>
  91. * @param ip The ip of the machine to connect to (in network byte order)
  92. * @return 0 in case of error, a value > 0 otherwise.
  93. */
  94. int smb_session_connect(smb_session *s, const char *name,
  95. uint32_t ip);
  96. /**
  97. * @brief Authenticate on the remote host with the provided credentials
  98. * @details Can be called if session state is SMB_STATE_DIALECT_OK.
  99. * If successfull, session state transition to SMB_STATE_SESSION_OK
  100. * @bug We currently only support LMv2 authentication.
  101. *
  102. * @param s The session object.
  103. * @param domain The domain of the user. You should use the (netbios) uppercased
  104. * machine name, then try 'WORKGROUP'.
  105. * @param user The user name in the current locale
  106. * @param password The password in the current locale
  107. * @return 0 in case of failure, > 0 in case of success. Success doesn't mean
  108. * you are logged in with the user you requested. If guest are activated on
  109. * the remote host, when login fails, you are logged in as 'Guest'.
  110. */
  111. int smb_session_login(smb_session *s, const char *domain,
  112. const char *user, const char *password);
  113. /**
  114. * @brief Am i logged in as Guest ?
  115. *
  116. * @param s The session object
  117. * @return 1 -> Logged in as guest
  118. * 0 -> Logged in as regular user
  119. * -1 -> Error (not logged in, invalid session, etc.)
  120. */
  121. int smb_session_is_guest(smb_session *s);
  122. /**
  123. * @brief Returns the server name with the <XX> type
  124. *
  125. * @param s The session object
  126. * @return The server name or NULL. The memory is owned by the session object.
  127. */
  128. const char *smb_session_server_name(smb_session *s);
  129. #endif