slapd.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. """
  2. Utilities for starting up a test slapd server
  3. and talking to it with ldapsearch/ldapadd.
  4. """
  5. import sys, os, socket, time, subprocess, logging
  6. _log = logging.getLogger("slapd")
  7. def quote(s):
  8. '''Quotes the '"' and '\' characters in a string and surrounds with "..."'''
  9. return '"' + s.replace('\\','\\\\').replace('"','\\"') + '"'
  10. def mkdirs(path):
  11. """Creates the directory path unless it already exists"""
  12. if not os.access(os.path.join(path, os.path.curdir), os.F_OK):
  13. _log.debug("creating temp directory %s", path)
  14. os.mkdir(path)
  15. return path
  16. def delete_directory_content(path):
  17. for dirpath,dirnames,filenames in os.walk(path, topdown=False):
  18. for n in filenames:
  19. _log.info("remove %s", os.path.join(dirpath, n))
  20. os.remove(os.path.join(dirpath, n))
  21. for n in dirnames:
  22. _log.info("rmdir %s", os.path.join(dirpath, n))
  23. os.rmdir(os.path.join(dirpath, n))
  24. LOCALHOST = '127.0.0.1'
  25. def find_available_tcp_port(host=LOCALHOST):
  26. s = socket.socket()
  27. s.bind((host, 0))
  28. port = s.getsockname()[1]
  29. s.close()
  30. _log.info("Found available port %d", port)
  31. return port
  32. class Slapd:
  33. """
  34. Controller class for a slapd instance, OpenLDAP's server.
  35. This class creates a temporary data store for slapd, runs it
  36. on a private port, and initialises it with a top-level dc and
  37. the root user.
  38. When a reference to an instance of this class is lost, the slapd
  39. server is shut down.
  40. """
  41. _log = logging.getLogger("Slapd")
  42. # Use /var/tmp to placate apparmour on Ubuntu:
  43. PATH_TMPDIR = "/var/tmp/python-ldap-test"
  44. PATH_SBINDIR = "/usr/sbin"
  45. PATH_BINDIR = "/usr/bin"
  46. PATH_SCHEMA_CORE = "/etc/ldap/schema/core.schema"
  47. PATH_LDAPADD = os.path.join(PATH_BINDIR, "ldapadd")
  48. PATH_LDAPSEARCH = os.path.join(PATH_BINDIR, "ldapsearch")
  49. PATH_SLAPD = os.path.join(PATH_SBINDIR, "slapd")
  50. PATH_SLAPTEST = os.path.join(PATH_SBINDIR, "slaptest")
  51. # TODO add paths for other OSs
  52. def check_paths(cls):
  53. """
  54. Checks that the configured executable paths look valid.
  55. If they don't, then logs warning messages (not errors).
  56. """
  57. for name,path in (
  58. ("slapd", cls.PATH_SLAPD),
  59. ("ldapadd", cls.PATH_LDAPADD),
  60. ("ldapsearch", cls.PATH_LDAPSEARCH),
  61. ):
  62. cls._log.debug("checking %s executable at %s", name, path)
  63. if not os.access(path, os.X_OK):
  64. cls._log.warn("cannot find %s executable at %s", name, path)
  65. check_paths = classmethod(check_paths)
  66. def __init__(self):
  67. self._config = []
  68. self._proc = None
  69. self._port = 0
  70. self._tmpdir = self.PATH_TMPDIR
  71. self._dn_suffix = "dc=python-ldap,dc=org"
  72. self._root_cn = "Manager"
  73. self._root_password = "password"
  74. self._slapd_debug_level = 0
  75. # Setters
  76. def set_port(self, port):
  77. self._port = port
  78. def set_dn_suffix(self, dn):
  79. self._dn_suffix = dn
  80. def set_root_cn(self, cn):
  81. self._root_cn = cn
  82. def set_root_password(self, pw):
  83. self._root_password = pw
  84. def set_tmpdir(self, path):
  85. self._tmpdir = path
  86. def set_slapd_debug_level(self, level):
  87. self._slapd_debug_level = level
  88. def set_debug(self):
  89. self._log.setLevel(logging.DEBUG)
  90. self.set_slapd_debug_level('Any')
  91. # getters
  92. def get_url(self):
  93. return "ldap://%s:%d/" % self.get_address()
  94. def get_address(self):
  95. if self._port == 0:
  96. self._port = find_available_tcp_port(LOCALHOST)
  97. return (LOCALHOST, self._port)
  98. def get_dn_suffix(self):
  99. return self._dn_suffix
  100. def get_root_dn(self):
  101. return "cn=" + self._root_cn + "," + self.get_dn_suffix()
  102. def get_root_password(self):
  103. return self._root_password
  104. def get_tmpdir(self):
  105. return self._tmpdir
  106. def __del__(self):
  107. self.stop()
  108. def configure(self, cfg):
  109. """
  110. Appends slapd.conf configuration lines to cfg.
  111. Also re-initializes any backing storage.
  112. Feel free to subclass and override this method.
  113. """
  114. # Global
  115. cfg.append("include " + quote(self.PATH_SCHEMA_CORE))
  116. cfg.append("allow bind_v2")
  117. # Database
  118. ldif_dir = mkdirs(os.path.join(self.get_tmpdir(), "ldif-data"))
  119. delete_directory_content(ldif_dir) # clear it out
  120. cfg.append("database ldif")
  121. cfg.append("directory " + quote(ldif_dir))
  122. cfg.append("suffix " + quote(self.get_dn_suffix()))
  123. cfg.append("rootdn " + quote(self.get_root_dn()))
  124. cfg.append("rootpw " + quote(self.get_root_password()))
  125. def _write_config(self):
  126. """Writes the slapd.conf file out, and returns the path to it."""
  127. path = os.path.join(self._tmpdir, "slapd.conf")
  128. ldif_dir = mkdirs(self._tmpdir)
  129. if os.access(path, os.F_OK):
  130. self._log.debug("deleting existing %s", path)
  131. os.remove(path)
  132. self._log.debug("writing config to %s", path)
  133. file(path, "w").writelines([line + "\n" for line in self._config])
  134. return path
  135. def start(self):
  136. """
  137. Starts the slapd server process running, and waits for it to come up.
  138. """
  139. if self._proc is None:
  140. ok = False
  141. config_path = None
  142. try:
  143. self.configure(self._config)
  144. self._test_configuration()
  145. self._start_slapd()
  146. self._wait_for_slapd()
  147. ok = True
  148. self._log.debug("slapd ready at %s", self.get_url())
  149. self.started()
  150. finally:
  151. if not ok:
  152. if config_path:
  153. try: os.remove(config_path)
  154. except os.error: pass
  155. if self._proc:
  156. self.stop()
  157. def _start_slapd(self):
  158. # Spawns/forks the slapd process
  159. config_path = self._write_config()
  160. self._log.info("starting slapd")
  161. self._proc = subprocess.Popen([self.PATH_SLAPD,
  162. "-f", config_path,
  163. "-h", self.get_url(),
  164. "-d", str(self._slapd_debug_level),
  165. ])
  166. self._proc_config = config_path
  167. def _wait_for_slapd(self):
  168. # Waits until the LDAP server socket is open, or slapd crashed
  169. s = socket.socket()
  170. while 1:
  171. if self._proc.poll() is not None:
  172. self._stopped()
  173. raise RuntimeError("slapd exited before opening port")
  174. try:
  175. self._log.debug("Connecting to %s", repr(self.get_address()))
  176. s.connect(self.get_address())
  177. s.close()
  178. return
  179. except socket.error:
  180. time.sleep(1)
  181. def stop(self):
  182. """Stops the slapd server, and waits for it to terminate"""
  183. if self._proc is not None:
  184. self._log.debug("stopping slapd")
  185. if hasattr(self._proc, 'terminate'):
  186. self._proc.terminate()
  187. else:
  188. import posix, signal
  189. posix.kill(self._proc.pid, signal.SIGHUP)
  190. #time.sleep(1)
  191. #posix.kill(self._proc.pid, signal.SIGTERM)
  192. #posix.kill(self._proc.pid, signal.SIGKILL)
  193. self.wait()
  194. def restart(self):
  195. """
  196. Restarts the slapd server; ERASING previous content.
  197. Starts the server even it if isn't already running.
  198. """
  199. self.stop()
  200. self.start()
  201. def wait(self):
  202. """Waits for the slapd process to terminate by itself."""
  203. if self._proc:
  204. self._proc.wait()
  205. self._stopped()
  206. def _stopped(self):
  207. """Called when the slapd server is known to have terminated"""
  208. if self._proc is not None:
  209. self._log.info("slapd terminated")
  210. self._proc = None
  211. try:
  212. os.remove(self._proc_config)
  213. except os.error:
  214. self._log.debug("could not remove %s", self._proc_config)
  215. def _test_configuration(self):
  216. config_path = self._write_config()
  217. try:
  218. self._log.debug("testing configuration")
  219. verboseflag = "-Q"
  220. if self._log.isEnabledFor(logging.DEBUG):
  221. verboseflag = "-v"
  222. p = subprocess.Popen([
  223. self.PATH_SLAPTEST,
  224. verboseflag,
  225. "-f", config_path
  226. ])
  227. if p.wait() != 0:
  228. raise RuntimeError("configuration test failed")
  229. self._log.debug("configuration seems ok")
  230. finally:
  231. os.remove(config_path)
  232. def ldapadd(self, ldif, extra_args=[]):
  233. """Runs ldapadd on this slapd instance, passing it the ldif content"""
  234. self._log.debug("adding %s", repr(ldif))
  235. p = subprocess.Popen([self.PATH_LDAPADD,
  236. "-x",
  237. "-D", self.get_root_dn(),
  238. "-w", self.get_root_password(),
  239. "-H", self.get_url()] + extra_args,
  240. stdin = subprocess.PIPE, stdout=subprocess.PIPE)
  241. p.communicate(ldif)
  242. if p.wait() != 0:
  243. raise RuntimeError("ldapadd process failed")
  244. def ldapsearch(self, base=None, filter='(objectClass=*)', attrs=[],
  245. scope='sub', extra_args=[]):
  246. if base is None: base = self.get_dn_suffix()
  247. self._log.debug("ldapsearch filter=%s", repr(filter))
  248. p = subprocess.Popen([self.PATH_LDAPSEARCH,
  249. "-x",
  250. "-D", self.get_root_dn(),
  251. "-w", self.get_root_password(),
  252. "-H", self.get_url(),
  253. "-b", base,
  254. "-s", scope,
  255. "-LL",
  256. ] + extra_args + [ filter ] + attrs,
  257. stdout = subprocess.PIPE)
  258. output = p.communicate()[0]
  259. if p.wait() != 0:
  260. raise RuntimeError("ldapadd process failed")
  261. # RFC 2849: LDIF format
  262. # unfold
  263. lines = []
  264. for l in output.split('\n'):
  265. if l.startswith(' '):
  266. lines[-1] = lines[-1] + l[1:]
  267. elif l == '' and lines and lines[-1] == '':
  268. pass # ignore multiple blank lines
  269. else:
  270. lines.append(l)
  271. # Remove comments
  272. lines = [l for l in lines if not l.startswith("#")]
  273. # Remove leading version and blank line(s)
  274. if lines and lines[0] == '': del lines[0]
  275. if not lines or lines[0] != 'version: 1':
  276. raise RuntimeError("expected 'version: 1', got " + repr(lines[:1]))
  277. del lines[0]
  278. if lines and lines[0] == '': del lines[0]
  279. # ensure the ldif ends with a blank line (unless it is just blank)
  280. if lines and lines[-1] != '': lines.append('')
  281. objects = []
  282. obj = []
  283. for line in lines:
  284. if line == '': # end of an object
  285. if obj[0][0] != 'dn':
  286. raise RuntimeError("first line not dn", repr(obj))
  287. objects.append((obj[0][1], obj[1:]))
  288. obj = []
  289. else:
  290. attr,value = line.split(':',2)
  291. if value.startswith(': '):
  292. value = base64.decodestring(value[2:])
  293. elif value.startswith(' '):
  294. value = value[1:]
  295. else:
  296. raise RuntimeError("bad line: " + repr(line))
  297. obj.append((attr,value))
  298. assert obj == []
  299. return objects
  300. def started(self):
  301. """
  302. This method is called when the LDAP server has started up and is empty.
  303. By default, this method adds the two initial objects,
  304. the domain object and the root user object.
  305. """
  306. assert self.get_dn_suffix().startswith("dc=")
  307. suffix_dc = self.get_dn_suffix().split(',')[0][3:]
  308. assert self.get_root_dn().startswith("cn=")
  309. assert self.get_root_dn().endswith("," + self.get_dn_suffix())
  310. root_cn = self.get_root_dn().split(',')[0][3:]
  311. self._log.debug("adding %s and %s",
  312. self.get_dn_suffix(),
  313. self.get_root_dn())
  314. self.ldapadd("\n".join([
  315. 'dn: ' + self.get_dn_suffix(),
  316. 'objectClass: dcObject',
  317. 'objectClass: organization',
  318. 'dc: ' + suffix_dc,
  319. 'o: ' + suffix_dc,
  320. '',
  321. 'dn: ' + self.get_root_dn(),
  322. 'objectClass: organizationalRole',
  323. 'cn: ' + root_cn,
  324. ''
  325. ]))
  326. Slapd.check_paths()
  327. if __name__ == '__main__' and sys.argv == ['run']:
  328. logging.basicConfig(level=logging.DEBUG)
  329. slapd = Slapd()
  330. print("Starting slapd...")
  331. slapd.start()
  332. print("Contents of LDAP server follow:\n")
  333. for dn,attrs in slapd.ldapsearch():
  334. print("dn: " + dn)
  335. for name,val in attrs:
  336. print(name + ": " + val)
  337. print("")
  338. print(slapd.get_url())
  339. slapd.wait()