Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ldifProducer.py 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. self._outstanding[message_id] = self.connection.request
  90. return message_id
  91. def post_send_single_response(self, message_id):
  92. self.connection.response = None
  93. self.connection.result = None
  94. if self._outstanding and message_id in self._outstanding:
  95. request = self._outstanding.pop(message_id)
  96. ldif_lines = operation_to_ldif(self.connection.request['type'], request, self.all_base64, self.order.get(self.connection.request['type']))
  97. if self.stream and ldif_lines and not self.connection.closed:
  98. self.accumulate_stream(self.line_separator.join(ldif_lines))
  99. ldif_lines = add_ldif_header(ldif_lines)
  100. self.connection.response = self.line_separator.join(ldif_lines)
  101. return self.connection.response
  102. return None
  103. def post_send_search(self, message_id):
  104. raise LDAPLDIFError('LDIF-CONTENT cannot be produced for Search operations')
  105. def _get_response(self, message_id):
  106. pass
  107. def accumulate_stream(self, fragment):
  108. if not self._header_added and self.stream.tell() == 0:
  109. self._header_added = True
  110. header = add_ldif_header(['-'])[0]
  111. self.stream.write(prepare_for_stream(header + self.line_separator + self.line_separator))
  112. self.stream.write(prepare_for_stream(fragment + self.line_separator + self.line_separator))
  113. def get_stream(self):
  114. return self.stream
  115. def set_stream(self, value):
  116. error = False
  117. try:
  118. if not value.writable():
  119. error = True
  120. except (ValueError, AttributeError):
  121. error = True
  122. if error:
  123. raise LDAPLDIFError('stream must be writable')
  124. self.stream = value