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.

syncrepl.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. # -*- coding: utf-8 -*-
  2. """
  3. ldap.syncrepl - for implementing syncrepl consumer (see RFC 4533)
  4. See https://www.python-ldap.org/ for project details.
  5. """
  6. from uuid import UUID
  7. # Imports from pyasn1
  8. from pyasn1.type import tag, namedtype, namedval, univ, constraint
  9. from pyasn1.codec.ber import encoder, decoder
  10. from ldap.pkginfo import __version__, __author__, __license__
  11. from ldap.controls import RequestControl, ResponseControl, KNOWN_RESPONSE_CONTROLS
  12. __all__ = [
  13. 'SyncreplConsumer',
  14. ]
  15. class SyncUUID(univ.OctetString):
  16. """
  17. syncUUID ::= OCTET STRING (SIZE(16))
  18. """
  19. subtypeSpec = constraint.ValueSizeConstraint(16, 16)
  20. class SyncCookie(univ.OctetString):
  21. """
  22. syncCookie ::= OCTET STRING
  23. """
  24. class SyncRequestMode(univ.Enumerated):
  25. """
  26. mode ENUMERATED {
  27. -- 0 unused
  28. refreshOnly (1),
  29. -- 2 reserved
  30. refreshAndPersist (3)
  31. },
  32. """
  33. namedValues = namedval.NamedValues(
  34. ('refreshOnly', 1),
  35. ('refreshAndPersist', 3)
  36. )
  37. subtypeSpec = univ.Enumerated.subtypeSpec + constraint.SingleValueConstraint(1, 3)
  38. class SyncRequestValue(univ.Sequence):
  39. """
  40. syncRequestValue ::= SEQUENCE {
  41. mode ENUMERATED {
  42. -- 0 unused
  43. refreshOnly (1),
  44. -- 2 reserved
  45. refreshAndPersist (3)
  46. },
  47. cookie syncCookie OPTIONAL,
  48. reloadHint BOOLEAN DEFAULT FALSE
  49. }
  50. """
  51. componentType = namedtype.NamedTypes(
  52. namedtype.NamedType('mode', SyncRequestMode()),
  53. namedtype.OptionalNamedType('cookie', SyncCookie()),
  54. namedtype.DefaultedNamedType('reloadHint', univ.Boolean(False))
  55. )
  56. class SyncRequestControl(RequestControl):
  57. """
  58. The Sync Request Control is an LDAP Control [RFC4511] where the
  59. controlType is the object identifier 1.3.6.1.4.1.4203.1.9.1.1 and the
  60. controlValue, an OCTET STRING, contains a BER-encoded
  61. syncRequestValue. The criticality field is either TRUE or FALSE.
  62. [..]
  63. The Sync Request Control is only applicable to the SearchRequest
  64. Message.
  65. """
  66. controlType = '1.3.6.1.4.1.4203.1.9.1.1'
  67. def __init__(self, criticality=1, cookie=None, mode='refreshOnly', reloadHint=False):
  68. self.criticality = criticality
  69. self.cookie = cookie
  70. self.mode = mode
  71. self.reloadHint = reloadHint
  72. def encodeControlValue(self):
  73. rcv = SyncRequestValue()
  74. rcv.setComponentByName('mode', SyncRequestMode(self.mode))
  75. if self.cookie is not None:
  76. rcv.setComponentByName('cookie', SyncCookie(self.cookie))
  77. if self.reloadHint:
  78. rcv.setComponentByName('reloadHint', univ.Boolean(self.reloadHint))
  79. return encoder.encode(rcv)
  80. class SyncStateOp(univ.Enumerated):
  81. """
  82. state ENUMERATED {
  83. present (0),
  84. add (1),
  85. modify (2),
  86. delete (3)
  87. },
  88. """
  89. namedValues = namedval.NamedValues(
  90. ('present', 0),
  91. ('add', 1),
  92. ('modify', 2),
  93. ('delete', 3)
  94. )
  95. subtypeSpec = univ.Enumerated.subtypeSpec + constraint.SingleValueConstraint(0, 1, 2, 3)
  96. class SyncStateValue(univ.Sequence):
  97. """
  98. syncStateValue ::= SEQUENCE {
  99. state ENUMERATED {
  100. present (0),
  101. add (1),
  102. modify (2),
  103. delete (3)
  104. },
  105. entryUUID syncUUID,
  106. cookie syncCookie OPTIONAL
  107. }
  108. """
  109. componentType = namedtype.NamedTypes(
  110. namedtype.NamedType('state', SyncStateOp()),
  111. namedtype.NamedType('entryUUID', SyncUUID()),
  112. namedtype.OptionalNamedType('cookie', SyncCookie())
  113. )
  114. class SyncStateControl(ResponseControl):
  115. """
  116. The Sync State Control is an LDAP Control [RFC4511] where the
  117. controlType is the object identifier 1.3.6.1.4.1.4203.1.9.1.2 and the
  118. controlValue, an OCTET STRING, contains a BER-encoded SyncStateValue.
  119. The criticality is FALSE.
  120. [..]
  121. The Sync State Control is only applicable to SearchResultEntry and
  122. SearchResultReference Messages.
  123. """
  124. controlType = '1.3.6.1.4.1.4203.1.9.1.2'
  125. opnames = ('present', 'add', 'modify', 'delete')
  126. def decodeControlValue(self, encodedControlValue):
  127. d = decoder.decode(encodedControlValue, asn1Spec=SyncStateValue())
  128. state = d[0].getComponentByName('state')
  129. uuid = UUID(bytes=bytes(d[0].getComponentByName('entryUUID')))
  130. cookie = d[0].getComponentByName('cookie')
  131. if cookie is not None and cookie.hasValue():
  132. self.cookie = str(cookie)
  133. else:
  134. self.cookie = None
  135. self.state = self.__class__.opnames[int(state)]
  136. self.entryUUID = str(uuid)
  137. KNOWN_RESPONSE_CONTROLS[SyncStateControl.controlType] = SyncStateControl
  138. class SyncDoneValue(univ.Sequence):
  139. """
  140. syncDoneValue ::= SEQUENCE {
  141. cookie syncCookie OPTIONAL,
  142. refreshDeletes BOOLEAN DEFAULT FALSE
  143. }
  144. """
  145. componentType = namedtype.NamedTypes(
  146. namedtype.OptionalNamedType('cookie', SyncCookie()),
  147. namedtype.DefaultedNamedType('refreshDeletes', univ.Boolean(False))
  148. )
  149. class SyncDoneControl(ResponseControl):
  150. """
  151. The Sync Done Control is an LDAP Control [RFC4511] where the
  152. controlType is the object identifier 1.3.6.1.4.1.4203.1.9.1.3 and the
  153. controlValue contains a BER-encoded syncDoneValue. The criticality
  154. is FALSE (and hence absent).
  155. [..]
  156. The Sync Done Control is only applicable to the SearchResultDone
  157. Message.
  158. """
  159. controlType = '1.3.6.1.4.1.4203.1.9.1.3'
  160. def decodeControlValue(self, encodedControlValue):
  161. d = decoder.decode(encodedControlValue, asn1Spec=SyncDoneValue())
  162. cookie = d[0].getComponentByName('cookie')
  163. if cookie.hasValue():
  164. self.cookie = str(cookie)
  165. else:
  166. self.cookie = None
  167. refresh_deletes = d[0].getComponentByName('refreshDeletes')
  168. if refresh_deletes.hasValue():
  169. self.refreshDeletes = bool(refresh_deletes)
  170. else:
  171. self.refreshDeletes = None
  172. KNOWN_RESPONSE_CONTROLS[SyncDoneControl.controlType] = SyncDoneControl
  173. class RefreshDelete(univ.Sequence):
  174. """
  175. refreshDelete [1] SEQUENCE {
  176. cookie syncCookie OPTIONAL,
  177. refreshDone BOOLEAN DEFAULT TRUE
  178. },
  179. """
  180. componentType = namedtype.NamedTypes(
  181. namedtype.OptionalNamedType('cookie', SyncCookie()),
  182. namedtype.DefaultedNamedType('refreshDone', univ.Boolean(True))
  183. )
  184. class RefreshPresent(univ.Sequence):
  185. """
  186. refreshPresent [2] SEQUENCE {
  187. cookie syncCookie OPTIONAL,
  188. refreshDone BOOLEAN DEFAULT TRUE
  189. },
  190. """
  191. componentType = namedtype.NamedTypes(
  192. namedtype.OptionalNamedType('cookie', SyncCookie()),
  193. namedtype.DefaultedNamedType('refreshDone', univ.Boolean(True))
  194. )
  195. class SyncUUIDs(univ.SetOf):
  196. """
  197. syncUUIDs SET OF syncUUID
  198. """
  199. componentType = SyncUUID()
  200. class SyncIdSet(univ.Sequence):
  201. """
  202. syncIdSet [3] SEQUENCE {
  203. cookie syncCookie OPTIONAL,
  204. refreshDeletes BOOLEAN DEFAULT FALSE,
  205. syncUUIDs SET OF syncUUID
  206. }
  207. """
  208. componentType = namedtype.NamedTypes(
  209. namedtype.OptionalNamedType('cookie', SyncCookie()),
  210. namedtype.DefaultedNamedType('refreshDeletes', univ.Boolean(False)),
  211. namedtype.NamedType('syncUUIDs', SyncUUIDs())
  212. )
  213. class SyncInfoValue(univ.Choice):
  214. """
  215. syncInfoValue ::= CHOICE {
  216. newcookie [0] syncCookie,
  217. refreshDelete [1] SEQUENCE {
  218. cookie syncCookie OPTIONAL,
  219. refreshDone BOOLEAN DEFAULT TRUE
  220. },
  221. refreshPresent [2] SEQUENCE {
  222. cookie syncCookie OPTIONAL,
  223. refreshDone BOOLEAN DEFAULT TRUE
  224. },
  225. syncIdSet [3] SEQUENCE {
  226. cookie syncCookie OPTIONAL,
  227. refreshDeletes BOOLEAN DEFAULT FALSE,
  228. syncUUIDs SET OF syncUUID
  229. }
  230. }
  231. """
  232. componentType = namedtype.NamedTypes(
  233. namedtype.NamedType(
  234. 'newcookie',
  235. SyncCookie().subtype(
  236. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)
  237. )
  238. ),
  239. namedtype.NamedType(
  240. 'refreshDelete',
  241. RefreshDelete().subtype(
  242. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)
  243. )
  244. ),
  245. namedtype.NamedType(
  246. 'refreshPresent',
  247. RefreshPresent().subtype(
  248. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2)
  249. )
  250. ),
  251. namedtype.NamedType(
  252. 'syncIdSet',
  253. SyncIdSet().subtype(
  254. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)
  255. )
  256. )
  257. )
  258. class SyncInfoMessage:
  259. """
  260. The Sync Info Message is an LDAP Intermediate Response Message
  261. [RFC4511] where responseName is the object identifier
  262. 1.3.6.1.4.1.4203.1.9.1.4 and responseValue contains a BER-encoded
  263. syncInfoValue. The criticality is FALSE (and hence absent).
  264. """
  265. responseName = '1.3.6.1.4.1.4203.1.9.1.4'
  266. def __init__(self, encodedMessage):
  267. d = decoder.decode(encodedMessage, asn1Spec=SyncInfoValue())
  268. self.newcookie = None
  269. self.refreshDelete = None
  270. self.refreshPresent = None
  271. self.syncIdSet = None
  272. for attr in ['newcookie', 'refreshDelete', 'refreshPresent', 'syncIdSet']:
  273. comp = d[0].getComponentByName(attr)
  274. if comp is not None and comp.hasValue():
  275. if attr == 'newcookie':
  276. self.newcookie = str(comp)
  277. return
  278. val = {}
  279. cookie = comp.getComponentByName('cookie')
  280. if cookie.hasValue():
  281. val['cookie'] = str(cookie)
  282. if attr.startswith('refresh'):
  283. val['refreshDone'] = bool(comp.getComponentByName('refreshDone'))
  284. elif attr == 'syncIdSet':
  285. uuids = []
  286. ids = comp.getComponentByName('syncUUIDs')
  287. for i in range(len(ids)):
  288. uuid = UUID(bytes=bytes(ids.getComponentByPosition(i)))
  289. uuids.append(str(uuid))
  290. val['syncUUIDs'] = uuids
  291. val['refreshDeletes'] = bool(comp.getComponentByName('refreshDeletes'))
  292. setattr(self, attr, val)
  293. return
  294. class SyncreplConsumer:
  295. """
  296. SyncreplConsumer - LDAP syncrepl consumer object.
  297. """
  298. def syncrepl_search(self, base, scope, mode='refreshOnly', cookie=None, **search_args):
  299. """
  300. Starts syncrepl search operation.
  301. base, scope, and search_args are passed along to
  302. self.search_ext unmodified (aside from adding a Sync
  303. Request control to any serverctrls provided).
  304. mode provides syncrepl mode. Can be 'refreshOnly'
  305. to finish after synchronization, or
  306. 'refreshAndPersist' to persist (continue to
  307. receive updates) after synchronization.
  308. cookie: an opaque value representing the replication
  309. state of the client. Subclasses should override
  310. the syncrepl_set_cookie() and syncrepl_get_cookie()
  311. methods to store the cookie appropriately, rather than
  312. passing it.
  313. Only a single syncrepl search may be active on a SyncreplConsumer
  314. object. Multiple concurrent syncrepl searches require multiple
  315. separate SyncreplConsumer objects and thus multiple connections
  316. (LDAPObject instances).
  317. """
  318. if cookie is None:
  319. cookie = self.syncrepl_get_cookie()
  320. syncreq = SyncRequestControl(cookie=cookie, mode=mode)
  321. if 'serverctrls' in search_args:
  322. search_args['serverctrls'] += [syncreq]
  323. else:
  324. search_args['serverctrls'] = [syncreq]
  325. self.__refreshDone = False
  326. return self.search_ext(base, scope, **search_args)
  327. def syncrepl_poll(self, msgid=-1, timeout=None, all=0):
  328. """
  329. polls for and processes responses to the syncrepl_search() operation.
  330. Returns False when operation finishes, True if it is in progress, or
  331. raises an exception on error.
  332. If timeout is specified, raises ldap.TIMEOUT in the event of a timeout.
  333. If all is set to a nonzero value, poll() will return only when finished
  334. or when an exception is raised.
  335. """
  336. while True:
  337. type, msg, mid, ctrls, n, v = self.result4(
  338. msgid=msgid,
  339. timeout=timeout,
  340. add_intermediates=1,
  341. add_ctrls=1,
  342. all=0,
  343. )
  344. if type == 101:
  345. # search result. This marks the end of a refreshOnly session.
  346. # look for a SyncDone control, save the cookie, and if necessary
  347. # delete non-present entries.
  348. for c in ctrls:
  349. if c.__class__.__name__ != 'SyncDoneControl':
  350. continue
  351. self.syncrepl_present(None, refreshDeletes=c.refreshDeletes)
  352. if c.cookie is not None:
  353. self.syncrepl_set_cookie(c.cookie)
  354. return False
  355. elif type == 100:
  356. # search entry with associated SyncState control
  357. for m in msg:
  358. dn, attrs, ctrls = m
  359. for c in ctrls:
  360. if c.__class__.__name__ != 'SyncStateControl':
  361. continue
  362. if c.state == 'present':
  363. self.syncrepl_present([c.entryUUID])
  364. elif c.state == 'delete':
  365. self.syncrepl_delete([c.entryUUID])
  366. else:
  367. self.syncrepl_entry(dn, attrs, c.entryUUID)
  368. if self.__refreshDone is False:
  369. self.syncrepl_present([c.entryUUID])
  370. if c.cookie is not None:
  371. self.syncrepl_set_cookie(c.cookie)
  372. break
  373. elif type == 121:
  374. # Intermediate message. If it is a SyncInfoMessage, parse it
  375. for m in msg:
  376. rname, resp, ctrls = m
  377. if rname != SyncInfoMessage.responseName:
  378. continue
  379. sim = SyncInfoMessage(resp)
  380. if sim.newcookie is not None:
  381. self.syncrepl_set_cookie(sim.newcookie)
  382. elif sim.refreshPresent is not None:
  383. self.syncrepl_present(None, refreshDeletes=False)
  384. if 'cookie' in sim.refreshPresent:
  385. self.syncrepl_set_cookie(sim.refreshPresent['cookie'])
  386. if sim.refreshPresent['refreshDone']:
  387. self.__refreshDone = True
  388. self.syncrepl_refreshdone()
  389. elif sim.refreshDelete is not None:
  390. self.syncrepl_present(None, refreshDeletes=True)
  391. if 'cookie' in sim.refreshDelete:
  392. self.syncrepl_set_cookie(sim.refreshDelete['cookie'])
  393. if sim.refreshDelete['refreshDone']:
  394. self.__refreshDone = True
  395. self.syncrepl_refreshdone()
  396. elif sim.syncIdSet is not None:
  397. if sim.syncIdSet['refreshDeletes'] is True:
  398. self.syncrepl_delete(sim.syncIdSet['syncUUIDs'])
  399. else:
  400. self.syncrepl_present(sim.syncIdSet['syncUUIDs'])
  401. if 'cookie' in sim.syncIdSet:
  402. self.syncrepl_set_cookie(sim.syncIdSet['cookie'])
  403. if all == 0:
  404. return True
  405. # virtual methods -- subclass must override these to do useful work
  406. def syncrepl_set_cookie(self, cookie):
  407. """
  408. Called by syncrepl_poll() to store a new cookie provided by the server.
  409. """
  410. pass
  411. def syncrepl_get_cookie(self):
  412. """
  413. Called by syncrepl_search() to retrieve the cookie stored by syncrepl_set_cookie()
  414. """
  415. pass
  416. def syncrepl_present(self, uuids, refreshDeletes=False):
  417. """
  418. Called by syncrepl_poll() whenever entry UUIDs are presented to the client.
  419. syncrepl_present() is given a list of entry UUIDs (uuids) and a flag
  420. (refreshDeletes) which indicates whether the server explicitly deleted
  421. non-present entries during the refresh operation.
  422. If called with a list of uuids, the syncrepl_present() implementation
  423. should record those uuids as present in the directory.
  424. If called with uuids set to None and refreshDeletes set to False,
  425. syncrepl_present() should delete all non-present entries from the local
  426. mirror, and reset the list of recorded uuids.
  427. If called with uuids set to None and refreshDeletes set to True,
  428. syncrepl_present() should reset the list of recorded uuids, without
  429. deleting any entries.
  430. """
  431. pass
  432. def syncrepl_delete(self, uuids):
  433. """
  434. Called by syncrepl_poll() to delete entries. A list
  435. of UUIDs of the entries to be deleted is given in the
  436. uuids parameter.
  437. """
  438. pass
  439. def syncrepl_entry(self, dn, attrs, uuid):
  440. """
  441. Called by syncrepl_poll() for any added or modified entries.
  442. The provided uuid is used to identify the provided entry in
  443. any future modification (including dn modification), deletion,
  444. and presentation operations.
  445. """
  446. pass
  447. def syncrepl_refreshdone(self):
  448. """
  449. Called by syncrepl_poll() between refresh and persist phase.
  450. It indicates that initial synchronization is done and persist phase
  451. follows.
  452. """
  453. pass