25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

151 lines
5.6 KiB

  1. """
  2. """
  3. # Created on 2013.07.15
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2013 - 2018 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. from io import StringIO
  25. from os import linesep
  26. import random
  27. from ..core.exceptions import LDAPLDIFError
  28. from ..utils.conv import prepare_for_stream
  29. from ..protocol.rfc4511 import LDAPMessage, MessageID, ProtocolOp, LDAP_MAX_INT
  30. from ..protocol.rfc2849 import operation_to_ldif, add_ldif_header
  31. from ..protocol.convert import build_controls_list
  32. from .base import BaseStrategy
  33. class LdifProducerStrategy(BaseStrategy):
  34. """
  35. This strategy is used to create the LDIF stream for the Add, Delete, Modify, ModifyDn operations.
  36. You send the request and get the request in the ldif-change representation of the operation.
  37. NO OPERATION IS SENT TO THE LDAP SERVER!
  38. Connection.request will contain the result LDAP message in a dict form
  39. Connection.response will contain the ldif-change format of the requested operation if available
  40. You don't need a real server to connect to for this strategy
  41. """
  42. def __init__(self, ldap_connection):
  43. BaseStrategy.__init__(self, ldap_connection)
  44. self.sync = True
  45. self.no_real_dsa = True
  46. self.pooled = False
  47. self.can_stream = True
  48. self.line_separator = linesep
  49. self.all_base64 = False
  50. self.stream = None
  51. self.order = dict()
  52. self._header_added = False
  53. random.seed()
  54. def _open_socket(self, address, use_ssl=False, unix_socket=False): # fake open socket
  55. self.connection.socket = NotImplemented # placeholder for a dummy socket
  56. if self.connection.usage:
  57. self.connection._usage.open_sockets += 1
  58. self.connection.closed = False
  59. def _close_socket(self):
  60. if self.connection.usage:
  61. self.connection._usage.closed_sockets += 1
  62. self.connection.socket = None
  63. self.connection.closed = True
  64. def _start_listen(self):
  65. self.connection.listening = True
  66. self.connection.closed = False
  67. self._header_added = False
  68. if not self.stream or (isinstance(self.stream, StringIO) and self.stream.closed):
  69. self.set_stream(StringIO())
  70. def _stop_listen(self):
  71. self.stream.close()
  72. self.connection.listening = False
  73. self.connection.closed = True
  74. def receiving(self):
  75. return None
  76. def send(self, message_type, request, controls=None):
  77. """
  78. Build the LDAPMessage without sending to server
  79. """
  80. message_id = random.randint(0, LDAP_MAX_INT)
  81. ldap_message = LDAPMessage()
  82. ldap_message['messageID'] = MessageID(message_id)
  83. ldap_message['protocolOp'] = ProtocolOp().setComponentByName(message_type, request)
  84. message_controls = build_controls_list(controls)
  85. if message_controls is not None:
  86. ldap_message['controls'] = message_controls
  87. self.connection.request = BaseStrategy.decode_request(message_type, request, controls)
  88. self.connection.request['controls'] = controls
  89. if self._outstanding is None:
  90. self._outstanding = dict()
  91. self._outstanding[message_id] = self.connection.request
  92. return message_id
  93. def post_send_single_response(self, message_id):
  94. self.connection.response = None
  95. self.connection.result = None
  96. if self._outstanding and message_id in self._outstanding:
  97. request = self._outstanding.pop(message_id)
  98. ldif_lines = operation_to_ldif(self.connection.request['type'], request, self.all_base64, self.order.get(self.connection.request['type']))
  99. if self.stream and ldif_lines and not self.connection.closed:
  100. self.accumulate_stream(self.line_separator.join(ldif_lines))
  101. ldif_lines = add_ldif_header(ldif_lines)
  102. self.connection.response = self.line_separator.join(ldif_lines)
  103. return self.connection.response
  104. return None
  105. def post_send_search(self, message_id):
  106. raise LDAPLDIFError('LDIF-CONTENT cannot be produced for Search operations')
  107. def _get_response(self, message_id):
  108. pass
  109. def accumulate_stream(self, fragment):
  110. if not self._header_added and self.stream.tell() == 0:
  111. self._header_added = True
  112. header = add_ldif_header(['-'])[0]
  113. self.stream.write(prepare_for_stream(header + self.line_separator + self.line_separator))
  114. self.stream.write(prepare_for_stream(fragment + self.line_separator + self.line_separator))
  115. def get_stream(self):
  116. return self.stream
  117. def set_stream(self, value):
  118. error = False
  119. try:
  120. if not value.writable():
  121. error = True
  122. except (ValueError, AttributeError):
  123. error = True
  124. if error:
  125. raise LDAPLDIFError('stream must be writable')
  126. self.stream = value