smb_session.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_session_send_msg(smb_session *s, smb_message *msg);
  77. // msg->packet will be updated to point on received data. You don't own this
  78. // memory. It'll be reused on next recv_msg
  79. ssize_t smb_session_recv_msg(smb_session *s, smb_message *msg);
  80. // --------------------------------
  81. /**
  82. * @brief Establish a connection and negotiate a session protocol with a remote
  83. * host
  84. * @details You have to provide both the ip and the name. This is a constraint
  85. * of Netbios, which requires you to know its name before he accepts to speak
  86. * with you.
  87. *
  88. * @param s A session object.
  89. * @param name The ASCII netbios name, the name type will be coerced to <20>
  90. * @param ip The ip of the machine to connect to (in network byte order)
  91. * @return 0 in case of error, a value > 0 otherwise.
  92. */
  93. int smb_session_connect(smb_session *s, const char *name,
  94. uint32_t ip, int transport);
  95. /**
  96. * @brief Authenticate on the remote host with the provided credentials
  97. * @details Can be called if session state is SMB_STATE_DIALECT_OK.
  98. * If successfull, session state transition to SMB_STATE_SESSION_OK
  99. * @bug We currently only support LMv2 authentication.
  100. *
  101. * @param s The session object.
  102. * @param domain The domain of the user. You should use the (netbios) uppercased
  103. * machine name, then try 'WORKGROUP'.
  104. * @param user The user name in the current locale
  105. * @param password The password in the current locale
  106. * @return 0 in case of failure, > 0 in case of success. Success doesn't mean
  107. * you are logged in with the user you requested. If guest are activated on
  108. * the remote host, when login fails, you are logged in as 'Guest'.
  109. */
  110. int smb_session_login(smb_session *s, const char *domain,
  111. const char *user, const char *password);
  112. /**
  113. * @brief Am i logged in as Guest ?
  114. *
  115. * @param s The session object
  116. * @return 1 -> Logged in as guest
  117. * 0 -> Logged in as regular user
  118. * -1 -> Error (not logged in, invalid session, etc.)
  119. */
  120. int smb_session_is_guest(smb_session *s);
  121. /**
  122. * @brief Returns the server name with the <XX> type
  123. *
  124. * @param s The session object
  125. * @return The server name or NULL. The memory is owned by the session object.
  126. */
  127. const char *smb_session_server_name(smb_session *s);
  128. #endif