ldap_admin.xml 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. <?xml version="1.0" encoding='ISO-8859-1'?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
  4. <!-- Include general documentation entities -->
  5. <!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
  6. %docentities;
  7. ]>
  8. <chapter>
  9. <title>&adminguide;</title>
  10. <section>
  11. <title>Overview</title>
  12. <para>The LDAP module implements an LDAP search interface for &kamailio;.
  13. It exports script functions to perform an LDAP search operation and to
  14. store the search results as &kamailio; AVPs. This allows for using LDAP
  15. directory data in the &kamailio; SIP message routing script.</para>
  16. <para>The following features are offered by the LDAP module:</para>
  17. <itemizedlist>
  18. <listitem>
  19. <para>LDAP search function based on a LDAP URL</para>
  20. </listitem>
  21. <listitem>
  22. <para>LDAP result parsing functions to store LDAP data as AVP variables</para>
  23. </listitem>
  24. <listitem>
  25. <para>Support for accessing multiple LDAP servers</para>
  26. </listitem>
  27. <listitem>
  28. <para>LDAP SIMPLE authentication</para>
  29. </listitem>
  30. <listitem>
  31. <para>LDAP server failover and automatic reconnect</para>
  32. </listitem>
  33. <listitem>
  34. <para>Configurable LDAP connection and bind timeouts</para>
  35. </listitem>
  36. <listitem>
  37. <para>Module API for LDAP search operations that can be used by other &kamailio; modules</para>
  38. </listitem>
  39. </itemizedlist>
  40. <para>The module implementation makes use of the open source <emphasis>OpenLDAP</emphasis> library available
  41. on most UNIX/Linux platforms. Besides LDAP server failover and automatic reconnect, this module can handle
  42. multiple LDAP sessions concurrently allowing access to data stored on different LDAP servers. Each &kamailio;
  43. worker process maintains one LDAP TCP connection per configured LDAP server. This enables parallel execution
  44. of LDAP requests and offloads LDAP concurrency control to the LDAP server(s).</para>
  45. <para>An LDAP search module API is provided that can be used by other &kamailio; modules. A module using this
  46. API does not have to implement LDAP connection management and configuration, while still having access
  47. to the full OpenLDAP API for searching and result handling.</para>
  48. <para>Since LDAP server implementations are optimized for fast read access they are a good choice to store SIP
  49. provisioning data. Performance tests have shown that this module achieves lower data access times and higher
  50. call rates than other database modules like e.g. the &kamailio; MYSQL module.</para>
  51. <section>
  52. <title>Usage Basics</title>
  53. <para>
  54. LDAP sessions is specified in an external configuration file (as described in
  55. <xref linkend="ldap-config" xreflabel="LDAP Configuration File"/>). Each of these LDAP sessions includes
  56. LDAP server access parameters like server hostname or connection timeouts. Normally only a single LDAP
  57. session per process will be used unless there is a need to access more than one LDAP server. The LDAP
  58. session name will then be used in the &kamailio; configuration script to refer to a specific LDAP session.
  59. </para>
  60. <para>
  61. The <varname>ldap_search</varname> function (<xref linkend="ldap-search-fn"/>) performs an LDAP search
  62. operation. It expects an LDAP URL as input which includes the LDAP session name and search parameters.
  63. The section <xref linkend="ldap-urls"/> provides a quick overview on LDAP URLs.
  64. </para>
  65. <para>
  66. The result of a LDAP search is stored internally and can be accessed with one of the
  67. <varname>ldap_result*</varname> functions. <varname>ldap_result</varname> (<xref linkend="ldap-result-fn"/>)
  68. stores resulting LDAP attribute values as AVPs. <varname>ldap_result_check</varname>
  69. (<xref linkend="ldap-result-check-fn"/>) is a convenience function to compare a string with LDAP attribute
  70. values using regular expression matching. Finally, <varname>ldap_result_next</varname>
  71. (<xref linkend="ldap-result-next-fn"/>) allows using LDAP search queries that return more than one LDAP entry.
  72. </para>
  73. <para>
  74. All <varname>ldap_result*</varname> functions always access the LDAP result set from the last
  75. <varname>ldap_search</varname> call. This should be kept in mind when calling <varname>ldap_search</varname>
  76. more than once in the &kamailio; configuration script.
  77. </para>
  78. </section>
  79. <section id="ldap-urls">
  80. <title>LDAP URLs</title>
  81. <para>
  82. <varname>ldap_search</varname> expects an LDAP URL as argument. This section describes the format and semantics of
  83. an LDAP URL.
  84. </para>
  85. <para>
  86. RFC 4516 <xref linkend="RFC4516"/> describes the format of an LDAP Uniform Resource Locator (URL). An LDAP URL
  87. represents an LDAP search operation in a compact format. The LDAP URL format is defined as follows (slightly
  88. modified, refer to section 2 of <xref linkend="RFC4516"/> for ABNF notation):
  89. </para>
  90. <blockquote>
  91. <para><literal>ldap://[ldap_session_name][/dn?attrs[?scope[?filter]]]]</literal></para>
  92. </blockquote>
  93. <variablelist>
  94. <varlistentry>
  95. <term><parameter>ldap_session_name</parameter></term>
  96. <listitem>
  97. <para>An LDAP session name as defined in the LDAP
  98. configuration file.</para>
  99. <para>(RFC 4516 defines this as LDAP hostport parameter)</para>
  100. </listitem>
  101. </varlistentry>
  102. <varlistentry>
  103. <term><parameter>dn</parameter></term>
  104. <listitem>
  105. <para>Base Distinguished Name (DN) of LDAP search or target of
  106. non-search operation, as defined in RFC 4514 <xref linkend="RFC4514"/></para>
  107. </listitem>
  108. </varlistentry>
  109. <varlistentry>
  110. <term><parameter>attrs</parameter></term>
  111. <listitem>
  112. <para>Comma separated list of LDAP attributes to be returned</para>
  113. </listitem>
  114. </varlistentry>
  115. <varlistentry>
  116. <term><parameter>scope</parameter></term>
  117. <listitem>
  118. <para>Scope for LDAP search, valid values are
  119. <quote>base</quote>, <quote>one</quote>, or
  120. <quote>sub</quote></para>
  121. </listitem>
  122. </varlistentry>
  123. <varlistentry>
  124. <term><parameter>filter</parameter></term>
  125. <listitem>
  126. <para>LDAP search filter definition following rules of RFC 4515
  127. <xref linkend="RFC4515"/><note>
  128. <para>The following table lists characters that have to be
  129. escaped in LDAP search filters:</para>
  130. <table>
  131. <title>RFC 4515 Escaping Rules</title>
  132. <tgroup cols="2">
  133. <tbody>
  134. <row>
  135. <entry><constant>*</constant></entry>
  136. <entry><constant>\2a</constant></entry>
  137. </row>
  138. <row>
  139. <entry><constant>(</constant></entry>
  140. <entry><constant>\28</constant></entry>
  141. </row>
  142. <row>
  143. <entry><constant>)</constant></entry>
  144. <entry><constant>\29</constant></entry>
  145. </row>
  146. <row>
  147. <entry><constant>\</constant></entry>
  148. <entry><constant>\5c</constant></entry>
  149. </row>
  150. </tbody>
  151. </tgroup>
  152. </table>
  153. </note></para>
  154. </listitem>
  155. </varlistentry>
  156. </variablelist>
  157. <note>
  158. <para>Non-URL characters in an LDAP URL have to be escaped using
  159. percent-encoding (refer to section 2.1 of RFC 4516). In particular
  160. this means that any "?" character in an LDAP URL component must be
  161. written as "%3F", since "?" is used as a URL delimiter.</para>
  162. <para>The exported function <varname>ldap_filter_url_encode</varname>
  163. (<xref linkend="ldap-filter-url-encode-fn"/>)
  164. implements RFC 4515/4516 LDAP search filter and URL escaping
  165. rules.</para>
  166. </note>
  167. </section>
  168. </section>
  169. <section>
  170. <title>Dependencies</title>
  171. <section>
  172. <title>&kamailio; Modules</title>
  173. <para>The module depends on the following modules (the listed modules
  174. must be loaded before this module):</para>
  175. <itemizedlist>
  176. <listitem>
  177. <para><emphasis>No dependencies on other &kamailio; modules.</emphasis></para>
  178. </listitem>
  179. </itemizedlist>
  180. </section>
  181. <section>
  182. <title>External Libraries or Applications</title>
  183. <para>The following libraries or applications must be installed before
  184. running &kamailio; with this module loaded:</para>
  185. <itemizedlist>
  186. <listitem>
  187. <para>OpenLDAP library (libldap) v2.1 or greater, libldap header files
  188. (libldap-dev) are needed for compilation</para>
  189. <para>OpenSSL library if you compile your OpenLDAP library with SSL/TLS support.</para>
  190. </listitem>
  191. </itemizedlist>
  192. </section>
  193. </section>
  194. <section id="ldap-config">
  195. <title>LDAP Configuration File</title>
  196. <para>The module reads an external configuration file at module
  197. initialization time that includes LDAP session definitions.</para>
  198. <section>
  199. <title>Configuration File Syntax</title>
  200. <para>The configuration file follows the Windows INI file syntax,
  201. section names are enclosed in square brackets:<programlisting>[Section_Name]</programlisting>Any
  202. section can contain zero or more configuration key assignments of the
  203. form <programlisting>key = value ; comment</programlisting> Values can
  204. be given enclosed with quotes. If no quotes are present, the value is
  205. understood as containing all characters between the first and the last
  206. non-blank characters. Lines starting with a hash sign and blank lines
  207. are treated as comments.</para>
  208. <para>Each section describes one LDAP session that can be referred to
  209. in the &kamailio; configuration script. Using the section name as the
  210. host part of an LDAP URL tells the module to use the LDAP session
  211. specified in the respective section. An example LDAP session
  212. specification looks like:
  213. <programlisting>
  214. [example_ldap]
  215. ldap_server_url = "ldap://ldap1.example.com, ldap://ldap2.example.com"
  216. ldap_bind_dn = "cn=sip_proxy,ou=accounts,dc=example,dc=com"
  217. ldap_bind_password = "pwd"
  218. ldap_network_timeout = 500
  219. ldap_client_bind_timeout = 500
  220. </programlisting>
  221. The configuration keys are explained in the following section.
  222. This LDAP session can be referred to in the routing script by using an LDAP URL like
  223. e.g.<programlisting>ldap://example_ldap/cn=admin,dc=example,dc=com</programlisting>
  224. </para>
  225. </section>
  226. <section>
  227. <title>LDAP Session Settings</title>
  228. <variablelist>
  229. <varlistentry>
  230. <term>ldap_server_url (mandatory)</term>
  231. <listitem>
  232. <para>
  233. LDAP URL including fully qualified domain name or IP address
  234. of LDAP server optionally followed by a colon and TCP port to
  235. connect: <varname>ldap://&lt;FQDN/IP&gt;[:&lt;port&gt;]</varname>.
  236. Failover LDAP servers can be added, each separated by a comma.
  237. In the event of connection errors, the module tries to connect
  238. to servers in order of appearance. To connect over TLS/SSL, use ldaps://.
  239. </para>
  240. <para>Default value: none, this is a mandatory setting</para>
  241. <example>
  242. <title><varname>ldap_server_url</varname> examples</title>
  243. <programlisting format="linespecific">
  244. ldap_server_url = "ldap://localhost"
  245. ldap_server_url = "ldaps://ldap.example.com:7777"
  246. ldap_server_url = "ldap://ldap1.example.com,
  247. ldap://ldap2.example.com:80389"
  248. </programlisting>
  249. </example>
  250. </listitem>
  251. </varlistentry>
  252. <varlistentry>
  253. <term>ldap_version (optional)</term>
  254. <listitem>
  255. <para>Supported LDAP versions are 2 and 3.</para>
  256. <para>Default value: <varname>3</varname> (LDAPv3)</para>
  257. <example>
  258. <title><varname>ldap_version</varname> example</title>
  259. <programlisting format="linespecific">ldap_version = 2</programlisting>
  260. </example>
  261. </listitem>
  262. </varlistentry>
  263. <varlistentry>
  264. <term>ldap_bind_dn (optional)</term>
  265. <listitem>
  266. <para>Authentication user DN used to bind to LDAP server (module
  267. currently only supports SIMPLE_AUTH). Empty string enables
  268. anonymous LDAP bind.</para>
  269. <para>Default value: <quote></quote> (empty string --&gt;
  270. anonymous bind)</para>
  271. <example>
  272. <title><varname>ldap_bind_dn</varname> example</title>
  273. <programlisting format="linespecific">ldap_bind_dn = "cn=root,dc=example,dc=com";</programlisting>
  274. </example>
  275. </listitem>
  276. </varlistentry>
  277. <varlistentry>
  278. <term>ldap_bind_password (optional)</term>
  279. <listitem>
  280. <para>Authentication password used to bind to LDAP server
  281. (SIMPLE_AUTH). Empty string enables anonymous bind.</para>
  282. <para>Default value: <quote></quote> (empty string --&gt;
  283. anonymous bind)</para>
  284. <example>
  285. <title><varname>ldap_bind_password</varname> example</title>
  286. <programlisting format="linespecific">ldap_bind_password = "secret";</programlisting>
  287. </example>
  288. </listitem>
  289. </varlistentry>
  290. <varlistentry>
  291. <term>ldap_network_timeout (optional)</term>
  292. <listitem>
  293. <para>LDAP TCP connect timeout in milliseconds. Setting this
  294. parameter to a low value enables fast failover if <varname>ldap_server_url</varname> contains more
  295. than one LDAP server addresses.</para>
  296. <para>Default value: 1000 (one second)</para>
  297. <example>
  298. <title><varname>ldap_network_timeout</varname> example</title>
  299. <programlisting format="linespecific">ldap_network_timeout = 500 ; setting TCP timeout to 500 ms</programlisting>
  300. </example>
  301. </listitem>
  302. </varlistentry>
  303. <varlistentry>
  304. <term>ldap_client_bind_timeout (optional)</term>
  305. <listitem>
  306. <para>LDAP bind operation timeout in milliseconds.</para>
  307. <para>Default value: 1000 (one second)</para>
  308. <example>
  309. <title><varname>ldap_client_bind_timeout</varname>
  310. example</title>
  311. <programlisting format="linespecific">ldap_client_bind_timeout = 1000</programlisting>
  312. </example>
  313. </listitem>
  314. </varlistentry>
  315. </variablelist>
  316. </section>
  317. <section>
  318. <title>Configuration File Example</title>
  319. <para>The following configuration file example includes two LDAP
  320. session definitions that could be used e.g. for accessing H.350 data
  321. and do phone number to name mappings.</para>
  322. <example>
  323. <title>Example LDAP Configuration File</title>
  324. <programlisting>
  325. # LDAP session "sipaccounts":
  326. #
  327. # - using LDAPv3 (default)
  328. # - two redundant LDAP servers
  329. #
  330. [sipaccounts]
  331. ldap_server_url = "ldap://h350-1.example.com, ldap://h350-2.example.com"
  332. ldap_bind_dn = "cn=sip_proxy,ou=accounts,dc=example,dc=com"
  333. ldap_bind_password = "pwd"
  334. ldap_network_timeout = 500
  335. ldap_client_bind_timeout = 500
  336. # LDAP session "campus":
  337. #
  338. # - using LDAPv2
  339. # - anonymous bind
  340. #
  341. [campus]
  342. ldap_version = 2
  343. ldap_server_url = "ldap://ldap.example.com"
  344. ldap_network_timeout = 500
  345. ldap_client_bind_timeout = 500
  346. </programlisting>
  347. </example>
  348. </section>
  349. </section>
  350. <section>
  351. <title>Parameters</title>
  352. <section>
  353. <title>config_file (string)</title>
  354. <para>Full path to LDAP configuration file.</para>
  355. <para>Default value:
  356. <varname>/usr/local/etc/&kamailiobinary;/ldap.cfg</varname></para>
  357. <example>
  358. <title><varname>config_file</varname> parameter usage</title>
  359. <programlisting format="linespecific">
  360. modparam("ldap", "config_file", "/usr/local/etc/&kamailiobinary;/ldap.ini")
  361. </programlisting>
  362. </example>
  363. </section>
  364. </section>
  365. <section>
  366. <title>Functions</title>
  367. <section id="ldap-search-fn">
  368. <title>ldap_search(ldap_url)</title>
  369. <para>Performs an LDAP search operation using given LDAP URL and stores result
  370. internally for later retrieval by <varname>ldap_result*</varname> functions. If one or
  371. more LDAP entries are found the function returns the number of found
  372. entries which evaluates to TRUE in the &kamailio; configuration script.
  373. It returns <varname>-1</varname> (<varname>FALSE</varname>) in case no
  374. LDAP entry was found, and <varname>-2</varname>
  375. (<varname>FALSE</varname>) if an internal error like e.g. an LDAP
  376. error occurred.</para>
  377. <variablelist>
  378. <title>Function Parameters:</title>
  379. <varlistentry>
  380. <term><parameter>ldap_url</parameter></term>
  381. <listitem>
  382. <para>An LDAP URL defining the LDAP search operation (refer to
  383. <xref linkend="ldap-urls"/> for a description of the LDAP URL
  384. format). The hostport part must be one of the LDAP session names
  385. declared in the LDAP configuration script.</para>
  386. <para>&kamailio; pseudo variables and AVPs included in
  387. <varname>ldap_url</varname> do get substituted with their
  388. value.</para>
  389. <example>
  390. <title>Example Usage of ldap_url</title>
  391. <para>Search with LDAP session named
  392. <varname>sipaccounts</varname>, base
  393. <varname>ou=sip,dc=example,dc=com</varname>,
  394. <varname>one</varname> level deep using search filter
  395. <varname>(cn=schlatter)</varname> and returning all
  396. attributes:</para>
  397. <programlisting>ldap://sipaccounts/ou=sip,dc=example,dc=com??one?(cn=schlatter)</programlisting>
  398. <para>Subtree search with LDAP session named
  399. <varname>ldap1</varname>, base
  400. <varname>dc=example,dc=com</varname> using search filter
  401. <varname>(cn=$(avp(s:name)))</varname> and returning
  402. <varname>SIPIdentityUserName</varname> and
  403. <varname>SIPIdentityServiceLevel</varname> attributes</para>
  404. <programlisting>
  405. ldap://ldap_1/dc=example,dc=com?
  406. SIPIdentityUserName,SIPIdentityServiceLevel?sub?(cn=$(avp(s:name)))
  407. </programlisting>
  408. </example>
  409. </listitem>
  410. </varlistentry>
  411. </variablelist>
  412. <variablelist>
  413. <title>Return Values:</title>
  414. <varlistentry>
  415. <term><varname>n</varname> &gt; 0 (TRUE):</term>
  416. <listitem>
  417. <itemizedlist>
  418. <listitem>
  419. <para>Found <varname>n</varname> matching LDAP
  420. entries</para>
  421. </listitem>
  422. </itemizedlist>
  423. </listitem>
  424. </varlistentry>
  425. <varlistentry>
  426. <term><constant>-1</constant> (FALSE):</term>
  427. <listitem>
  428. <itemizedlist>
  429. <listitem>
  430. <para>No matching LDAP entries found</para>
  431. </listitem>
  432. </itemizedlist>
  433. </listitem>
  434. </varlistentry>
  435. <varlistentry>
  436. <term><constant>-2</constant> (FALSE):</term>
  437. <listitem>
  438. <itemizedlist>
  439. <listitem>
  440. <para>LDAP error (e.g. LDAP server unavailable), or</para>
  441. </listitem>
  442. <listitem>
  443. <para>internal error</para>
  444. </listitem>
  445. </itemizedlist>
  446. </listitem>
  447. </varlistentry>
  448. </variablelist>
  449. <para>
  450. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, BRANCH_ROUTE, and ONREPLY_ROUTE.
  451. </para>
  452. <example>
  453. <title>Example Usage</title>
  454. <programlisting>
  455. ...
  456. # ldap search
  457. if (!ldap_search("ldap://sipaccounts/ou=sip,dc=example,dc=com??one?(cn=$rU)"))
  458. {
  459. switch ($retcode)
  460. {
  461. case -1:
  462. # no LDAP entry found
  463. sl_send_reply("404", "User Not Found");
  464. exit;
  465. case -2:
  466. # internal error
  467. sl_send_reply("500", "Internal server error");
  468. exit;
  469. default:
  470. exit;
  471. }
  472. }
  473. xlog("L_INFO", "ldap_search: found [$retcode] entries for (cn=$rU)");
  474. # save telephone number in $avp(s:tel_number)
  475. ldap_result("telephoneNumber/$avp(s:tel_number)");
  476. ...
  477. </programlisting>
  478. </example>
  479. </section>
  480. <section id="ldap-result-fn">
  481. <title>ldap_result("ldap_attr_name/avp_spec[/avp_type]" [, regex_subst])</title>
  482. <para>This function converts LDAP attribute values into AVPs for later
  483. use in the message routing script. It accesses the LDAP result set
  484. fetched by the last <varname>ldap_search</varname> call.
  485. <varname>ldap_attr_name</varname> specifies the LDAP attribute name
  486. who's value will be stored in AVP <varname>avp_spec</varname>. Multi
  487. valued LDAP attributes generate an indexed AVP. The optional
  488. <varname>regex_subst</varname> parameter allows to further define what
  489. part of an attribute value should be stored as AVP.</para>
  490. <para>
  491. An AVP can either be of type string or integer. As default,
  492. <varname>ldap_result</varname> stores LDAP attribute values as AVP of type string.
  493. The optional <varname>avp_type</varname> parameter can be used to explicitly specify
  494. the type of the AVP. It can be either <varname>str</varname> for string, or
  495. <varname>int</varname> for integer. If <varname>avp_type</varname> is specified as
  496. <varname>int</varname> then <varname>ldap_result</varname> tries to convert the LDAP
  497. attribute values to integer. In this case, the values are only stored as AVP if the
  498. conversion to integer is succesfull.
  499. </para>
  500. <variablelist>
  501. <title>Function Parameters:</title>
  502. <varlistentry>
  503. <term>ldap_attr_name</term>
  504. <listitem>
  505. <para>The name of the LDAP attribute who's value should be
  506. stored, e.g. <varname>SIPIdentityServiceLevel</varname> or
  507. <varname>telephonenumber</varname></para>
  508. </listitem>
  509. </varlistentry>
  510. <varlistentry>
  511. <term>avp_spec</term>
  512. <listitem>
  513. <para>Specification of destination AVP, e.g.
  514. <varname>$avp(s:service_level)</varname> or
  515. <varname>$avp(i:12)</varname></para>
  516. </listitem>
  517. </varlistentry>
  518. <varlistentry>
  519. <term>avp_type</term>
  520. <listitem>
  521. <para>
  522. Opional specification of destination AVP type, either <varname>str</varname>
  523. or <varname>int</varname>. If this parameter is not specified then the LDAP
  524. attribute values are stored as AVP of type string.
  525. </para>
  526. </listitem>
  527. </varlistentry>
  528. <varlistentry>
  529. <term>regex_subst</term>
  530. <listitem>
  531. <para>Regex substitution that gets applied to LDAP attribute
  532. value before storing it as AVP, e.g.
  533. <varname>"/^sip:(.+)$/\1/"</varname> to strip off "sip:" from
  534. the beginning of an LDAP attribute value.</para>
  535. </listitem>
  536. </varlistentry>
  537. </variablelist>
  538. <variablelist>
  539. <title>Return Values:</title>
  540. <varlistentry>
  541. <term><varname>n</varname> &gt; 0 (TRUE)</term>
  542. <listitem>
  543. <para>
  544. LDAP attribute <varname>ldap_attr_name</varname> found in LDAP result
  545. set and <varname>n</varname> LDAP attribute values stored in
  546. <varname>avp_spec</varname>
  547. </para>
  548. </listitem>
  549. </varlistentry>
  550. <varlistentry>
  551. <term>-1 (FALSE)</term>
  552. <listitem>
  553. <para>No LDAP attribute <varname>ldap_attr_name</varname> found
  554. in LDAP result set</para>
  555. </listitem>
  556. </varlistentry>
  557. <varlistentry>
  558. <term>-2 (FALSE)</term>
  559. <listitem>
  560. <para>Internal error occurred</para>
  561. </listitem>
  562. </varlistentry>
  563. </variablelist>
  564. <para>
  565. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, BRANCH_ROUTE, and ONREPLY_ROUTE.
  566. </para>
  567. <example>
  568. <title>Example Usage</title>
  569. <programlisting>
  570. ...
  571. # ldap_search call
  572. ...
  573. # save SIPIdentityServiceLevel in $avp(s:service_level)
  574. if (!ldap_result("SIPIdentityServiceLevel/$avp(s:service_level)"))
  575. {
  576. switch ($retcode)
  577. {
  578. case -1:
  579. # no SIPIdentityServiceLevel found
  580. sl_send_reply("403", "Forbidden");
  581. exit;
  582. case -2:
  583. # internal error
  584. sl_send_reply("500", "Internal server error");
  585. exit;
  586. default:
  587. exit;
  588. }
  589. }
  590. # save SIP URI domain in $avp(i:10)
  591. ldap_result("SIPIdentitySIPURI/$avp(i:10)", "/^[^@]+@(.+)$/\1/");
  592. ...
  593. </programlisting>
  594. </example>
  595. </section>
  596. <section id="ldap-result-check-fn">
  597. <title>ldap_result_check("ldap_attr_name/string_to_match" [,
  598. regex_subst])</title>
  599. <para>This function compares <varname>ldap_attr_name</varname>'s value
  600. with <varname>string_to_match</varname> for equality. It accesses the
  601. LDAP result set fetched by the last <varname>ldap_search</varname> call. The
  602. optional <varname>regex_subst</varname> parameter allows to further
  603. define what part of the attribute value should be used for the
  604. equality match. If <varname>ldap_attr_name</varname> is multi valued,
  605. each value is checked against <varname>string_to_match</varname>. If
  606. one or more of the values do match the function returns <varname>1</varname>
  607. (TRUE).</para>
  608. <variablelist>
  609. <title>Function Parameters:</title>
  610. <varlistentry>
  611. <term>ldap_attr_name</term>
  612. <listitem>
  613. <para>The name of the LDAP attribute who's value should be
  614. matched, e.g. <varname>SIPIdentitySIPURI</varname></para>
  615. </listitem>
  616. </varlistentry>
  617. <varlistentry>
  618. <term>string_to_match</term>
  619. <listitem>
  620. <para>String to be matched. Included AVPs and pseudo variabels
  621. do get expanded.</para>
  622. </listitem>
  623. </varlistentry>
  624. <varlistentry>
  625. <term>regex_subst</term>
  626. <listitem>
  627. <para>Regex substitution that gets applied to LDAP attribute
  628. value before comparing it with string_to_match, e.g.
  629. <varname>"/^[^@]@+(.+)$/\1/"</varname> to extract the domain part
  630. of a SIP URI</para>
  631. </listitem>
  632. </varlistentry>
  633. </variablelist>
  634. <variablelist>
  635. <title>Return Values:</title>
  636. <varlistentry>
  637. <term>1 (TRUE)</term>
  638. <listitem>
  639. <para>One or more <varname>ldap_attr_name</varname> attribute values match
  640. <varname>string_to_match</varname> (after
  641. <varname>regex_subst</varname> is applied)</para>
  642. </listitem>
  643. </varlistentry>
  644. <varlistentry>
  645. <term>-1 (FALSE)</term>
  646. <listitem>
  647. <para><varname>ldap_attr_name</varname> attribute not found or
  648. attribute value doesn't match <varname>string_to_match</varname>
  649. (after <varname>regex_subst</varname> is applied)</para>
  650. </listitem>
  651. </varlistentry>
  652. <varlistentry>
  653. <term>-2 (FALSE)</term>
  654. <listitem>
  655. <para>Internal error occurred</para>
  656. </listitem>
  657. </varlistentry>
  658. </variablelist>
  659. <para>
  660. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, BRANCH_ROUTE, and ONREPLY_ROUTE.
  661. </para>
  662. <example>
  663. <title>Example Usage</title>
  664. <programlisting>
  665. ...
  666. # ldap_search call
  667. ...
  668. # check if 'sn' ldap attribute value equals username part of R-URI,
  669. # the same could be achieved with ldap_result_check("sn/$rU")
  670. if (!ldap_result_check("sn/$ru", "/^sip:([^@]).*$/\1/"))
  671. {
  672. switch ($retcode)
  673. {
  674. case -1:
  675. # R-URI username doesn't match sn
  676. sl_send_reply("401", "Unauthorized");
  677. exit;
  678. case -2:
  679. # internal error
  680. sl_send_reply("500", "Internal server error");
  681. exit;
  682. default:
  683. exit;
  684. }
  685. }
  686. ...
  687. </programlisting>
  688. </example>
  689. </section>
  690. <section id="ldap-result-next-fn">
  691. <title>ldap_result_next()</title>
  692. <para>An LDAP search operation can return multiple LDAP entries. This
  693. function can be used to cycle through all returned LDAP entries. It
  694. returns 1 (TRUE) if there is another LDAP entry present in the LDAP
  695. result set and causes <varname>ldap_result*</varname> functions to work on the next LDAP
  696. entry. The function returns -1 (FALSE) if there are no more LDAP
  697. entries in the LDAP result set.</para>
  698. <variablelist>
  699. <title>Return Values:</title>
  700. <varlistentry>
  701. <term>1 (TRUE)</term>
  702. <listitem>
  703. <para>Another LDAP entry is present in the LDAP result set and
  704. result pointer is incremented by one</para>
  705. </listitem>
  706. </varlistentry>
  707. <varlistentry>
  708. <term>-1 (FALSE)</term>
  709. <listitem>
  710. <para>No more LDAP entries are available</para>
  711. </listitem>
  712. </varlistentry>
  713. <varlistentry>
  714. <term><constant>-2</constant> (FALSE)</term>
  715. <listitem>
  716. <para>Internal error</para>
  717. </listitem>
  718. </varlistentry>
  719. </variablelist>
  720. <para>
  721. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, BRANCH_ROUTE, and ONREPLY_ROUTE.
  722. </para>
  723. <example>
  724. <title>Example Usage</title>
  725. <programlisting>
  726. ...
  727. # ldap_search call
  728. ...
  729. ldap_result("telephonenumber/$avp(s:tel1)");
  730. if (ldap_result_next())
  731. {
  732. ldap_result("telephonenumber/$avp(s:tel2)");
  733. }
  734. if (ldap_result_next())
  735. {
  736. ldap_result("telephonenumber/$avp(s:tel3)");
  737. }
  738. if (ldap_result_next())
  739. {
  740. ldap_result("telephonenumber/$avp(s:tel4)");
  741. }
  742. ...
  743. </programlisting>
  744. </example>
  745. </section>
  746. <section id="ldap-filter-url-encode-fn">
  747. <title>ldap_filter_url_encode(string, avp_spec)</title>
  748. <para>This function applies the following escaping rules to
  749. <varname>string</varname> and stores the result in AVP
  750. <varname>avp_spec</varname>:</para>
  751. <table>
  752. <title>ldap_filter_url_encode() escaping rules</title>
  753. <tgroup cols="3">
  754. <thead>
  755. <row>
  756. <entry align="center">character in
  757. <varname>string</varname></entry>
  758. <entry align="center">gets replaced with</entry>
  759. <entry align="center">defined in</entry>
  760. </row>
  761. </thead>
  762. <tbody>
  763. <row>
  764. <entry>*</entry>
  765. <entry>\2a</entry>
  766. <entry>RFC 4515</entry>
  767. </row>
  768. <row>
  769. <entry>(</entry>
  770. <entry>\28</entry>
  771. <entry>RFC 4515</entry>
  772. </row>
  773. <row>
  774. <entry>)</entry>
  775. <entry>\29</entry>
  776. <entry>RFC 4515</entry>
  777. </row>
  778. <row>
  779. <entry>\</entry>
  780. <entry>\5c</entry>
  781. <entry>RFC 4515</entry>
  782. </row>
  783. <row>
  784. <entry>?</entry>
  785. <entry>%3F</entry>
  786. <entry>RFC 4516</entry>
  787. </row>
  788. </tbody>
  789. </tgroup>
  790. </table>
  791. <para>The string stored in AVP <varname>avp_spec</varname> can be safely used in an LDAP
  792. URL filter string.</para>
  793. <variablelist>
  794. <title>Function Parameters:</title>
  795. <varlistentry>
  796. <term><parameter>string</parameter></term>
  797. <listitem>
  798. <para>String to apply RFC 4515 and URL escpaing rules to.
  799. AVPs and pseudo variables do get expanded. Example:
  800. <varname>"cn=$avp(s:name)"</varname></para>
  801. </listitem>
  802. </varlistentry>
  803. <varlistentry>
  804. <term><parameter>avp_spec</parameter></term>
  805. <listitem>
  806. <para>Specification of AVP to store resulting RFC 4515
  807. and URL encoded string, e.g. <varname>$avp(s:ldap_search)</varname>
  808. or <varname>$avp(i:10)</varname></para>
  809. </listitem>
  810. </varlistentry>
  811. </variablelist>
  812. <variablelist>
  813. <title>Return Values:</title>
  814. <varlistentry>
  815. <term><constant>1</constant> (TRUE)</term>
  816. <listitem>
  817. <para>RFC 4515 and URL encoded
  818. <varname>filter_component</varname> stored as AVP
  819. <varname>avp_name</varname></para>
  820. </listitem>
  821. </varlistentry>
  822. <varlistentry>
  823. <term><constant>-1</constant> (FALSE)</term>
  824. <listitem>
  825. <para>Internal error</para>
  826. </listitem>
  827. </varlistentry>
  828. </variablelist>
  829. <para>
  830. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, BRANCH_ROUTE, and ONREPLY_ROUTE.
  831. </para>
  832. <example>
  833. <title>Example Usage</title>
  834. <programlisting>
  835. ...
  836. if (!ldap_filter_url_encode("cn=$avp(s:name)", "$avp(s:name_esc)"))
  837. {
  838. # RFC 4515/URL encoding failed --> silently discard request
  839. exit;
  840. }
  841. xlog("L_INFO", "encoded LDAP filter component: [$avp(s:name_esc)]\n");
  842. if (ldap_search(
  843. "ldap://h350/ou=commObjects,dc=example,dc=com??sub?($avp(s:name_esc))"))
  844. { ... }
  845. ...
  846. </programlisting>
  847. </example>
  848. </section>
  849. </section>
  850. <section>
  851. <title>Installation &amp; Running</title>
  852. <section>
  853. <title>Compiling the LDAP module</title>
  854. <para>
  855. OpenLDAP library (libldap) and header files (libldap-dev) v2.1 or greater (this module was
  856. tested with v2.1.3 and v2.3.32) are required for compiling the LDAP module. The OpenLDAP
  857. source is available at <ulink url="http://www.openldap.org/">http://www.openldap.org/</ulink>.
  858. Note that TLS support needs to be added a compile time for the libraries.
  859. </para>
  860. <para>
  861. The OpenLDAP library is available pre-compiled for most UNIX/Linux flavors. On Debian/Ubuntu,
  862. the following packages must be installed: <programlisting># apt-get install libldap2 libldap2-dev</programlisting>.
  863. </para>
  864. </section>
  865. </section>
  866. </chapter>