ldap.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Amitay Isaacs <amitay@w-o-i.com> |
  16. | Eric Warnke <ericw@albany.edu> |
  17. | Rasmus Lerdorf <rasmus@php.net> |
  18. | Gerrit Thomson <334647@swin.edu.au> |
  19. | Jani Taskinen <sniper@iki.fi> |
  20. | Stig Venaas <venaas@uninett.no> |
  21. | Doug Goldstein <cardoe@cardoe.com> |
  22. | PHP 4.0 updates: Zeev Suraski <zeev@zend.com> |
  23. +----------------------------------------------------------------------+
  24. */
  25. /* $Id$ */
  26. #define IS_EXT_MODULE
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. /* Additional headers for NetWare */
  31. #if defined(NETWARE) && (NEW_LIBC)
  32. #include <sys/select.h>
  33. #include <sys/timeval.h>
  34. #endif
  35. #include "php.h"
  36. #include "php_ini.h"
  37. #include <stddef.h>
  38. #include "ext/standard/dl.h"
  39. #include "php_ldap.h"
  40. #ifdef PHP_WIN32
  41. #include <string.h>
  42. #include "config.w32.h"
  43. #if HAVE_NSLDAP
  44. #include <winsock2.h>
  45. #endif
  46. #define strdup _strdup
  47. #undef WINDOWS
  48. #undef strcasecmp
  49. #undef strncasecmp
  50. #define WINSOCK 1
  51. #define __STDC__ 1
  52. #endif
  53. #include "ext/standard/php_string.h"
  54. #include "ext/standard/info.h"
  55. #ifdef HAVE_LDAP_SASL_H
  56. #include <sasl.h>
  57. #elif defined(HAVE_LDAP_SASL_SASL_H)
  58. #include <sasl/sasl.h>
  59. #endif
  60. #define PHP_LDAP_ESCAPE_FILTER 0x01
  61. #define PHP_LDAP_ESCAPE_DN 0x02
  62. #if defined(LDAP_CONTROL_PAGEDRESULTS) && !defined(HAVE_LDAP_CONTROL_FIND)
  63. LDAPControl *ldap_control_find( const char *oid, LDAPControl **ctrls, LDAPControl ***nextctrlp)
  64. {
  65. assert(nextctrlp == NULL);
  66. return ldap_find_control(oid, ctrls);
  67. }
  68. #endif
  69. #if !defined(LDAP_API_FEATURE_X_OPENLDAP)
  70. void ldap_memvfree(void **v)
  71. {
  72. ldap_value_free((char **)v);
  73. }
  74. #endif
  75. typedef struct {
  76. LDAP *link;
  77. #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
  78. zval *rebindproc;
  79. #endif
  80. } ldap_linkdata;
  81. typedef struct {
  82. LDAPMessage *data;
  83. BerElement *ber;
  84. int id;
  85. } ldap_resultentry;
  86. ZEND_DECLARE_MODULE_GLOBALS(ldap)
  87. static PHP_GINIT_FUNCTION(ldap);
  88. static int le_link, le_result, le_result_entry;
  89. #ifdef COMPILE_DL_LDAP
  90. ZEND_GET_MODULE(ldap)
  91. #endif
  92. static void _close_ldap_link(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
  93. {
  94. ldap_linkdata *ld = (ldap_linkdata *)rsrc->ptr;
  95. ldap_unbind_ext(ld->link, NULL, NULL);
  96. #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
  97. if (ld->rebindproc != NULL) {
  98. zval_dtor(ld->rebindproc);
  99. FREE_ZVAL(ld->rebindproc);
  100. }
  101. #endif
  102. efree(ld);
  103. LDAPG(num_links)--;
  104. }
  105. /* }}} */
  106. static void _free_ldap_result(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
  107. {
  108. LDAPMessage *result = (LDAPMessage *)rsrc->ptr;
  109. ldap_msgfree(result);
  110. }
  111. /* }}} */
  112. static void _free_ldap_result_entry(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
  113. {
  114. ldap_resultentry *entry = (ldap_resultentry *)rsrc->ptr;
  115. if (entry->ber != NULL) {
  116. ber_free(entry->ber, 0);
  117. entry->ber = NULL;
  118. }
  119. zend_list_delete(entry->id);
  120. efree(entry);
  121. }
  122. /* }}} */
  123. /* {{{ PHP_INI_BEGIN
  124. */
  125. PHP_INI_BEGIN()
  126. STD_PHP_INI_ENTRY_EX("ldap.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_links, zend_ldap_globals, ldap_globals, display_link_numbers)
  127. PHP_INI_END()
  128. /* }}} */
  129. /* {{{ PHP_GINIT_FUNCTION
  130. */
  131. static PHP_GINIT_FUNCTION(ldap)
  132. {
  133. ldap_globals->num_links = 0;
  134. }
  135. /* }}} */
  136. /* {{{ PHP_MINIT_FUNCTION
  137. */
  138. PHP_MINIT_FUNCTION(ldap)
  139. {
  140. REGISTER_INI_ENTRIES();
  141. /* Constants to be used with deref-parameter in php_ldap_do_search() */
  142. REGISTER_LONG_CONSTANT("LDAP_DEREF_NEVER", LDAP_DEREF_NEVER, CONST_PERSISTENT | CONST_CS);
  143. REGISTER_LONG_CONSTANT("LDAP_DEREF_SEARCHING", LDAP_DEREF_SEARCHING, CONST_PERSISTENT | CONST_CS);
  144. REGISTER_LONG_CONSTANT("LDAP_DEREF_FINDING", LDAP_DEREF_FINDING, CONST_PERSISTENT | CONST_CS);
  145. REGISTER_LONG_CONSTANT("LDAP_DEREF_ALWAYS", LDAP_DEREF_ALWAYS, CONST_PERSISTENT | CONST_CS);
  146. /* Constants to be used with ldap_modify_batch() */
  147. REGISTER_LONG_CONSTANT("LDAP_MODIFY_BATCH_ADD", LDAP_MODIFY_BATCH_ADD, CONST_PERSISTENT | CONST_CS);
  148. REGISTER_LONG_CONSTANT("LDAP_MODIFY_BATCH_REMOVE", LDAP_MODIFY_BATCH_REMOVE, CONST_PERSISTENT | CONST_CS);
  149. REGISTER_LONG_CONSTANT("LDAP_MODIFY_BATCH_REMOVE_ALL", LDAP_MODIFY_BATCH_REMOVE_ALL, CONST_PERSISTENT | CONST_CS);
  150. REGISTER_LONG_CONSTANT("LDAP_MODIFY_BATCH_REPLACE", LDAP_MODIFY_BATCH_REPLACE, CONST_PERSISTENT | CONST_CS);
  151. REGISTER_STRING_CONSTANT("LDAP_MODIFY_BATCH_ATTRIB", LDAP_MODIFY_BATCH_ATTRIB, CONST_PERSISTENT | CONST_CS);
  152. REGISTER_STRING_CONSTANT("LDAP_MODIFY_BATCH_MODTYPE", LDAP_MODIFY_BATCH_MODTYPE, CONST_PERSISTENT | CONST_CS);
  153. REGISTER_STRING_CONSTANT("LDAP_MODIFY_BATCH_VALUES", LDAP_MODIFY_BATCH_VALUES, CONST_PERSISTENT | CONST_CS);
  154. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP
  155. /* LDAP options */
  156. REGISTER_LONG_CONSTANT("LDAP_OPT_DEREF", LDAP_OPT_DEREF, CONST_PERSISTENT | CONST_CS);
  157. REGISTER_LONG_CONSTANT("LDAP_OPT_SIZELIMIT", LDAP_OPT_SIZELIMIT, CONST_PERSISTENT | CONST_CS);
  158. REGISTER_LONG_CONSTANT("LDAP_OPT_TIMELIMIT", LDAP_OPT_TIMELIMIT, CONST_PERSISTENT | CONST_CS);
  159. #ifdef LDAP_OPT_NETWORK_TIMEOUT
  160. REGISTER_LONG_CONSTANT("LDAP_OPT_NETWORK_TIMEOUT", LDAP_OPT_NETWORK_TIMEOUT, CONST_PERSISTENT | CONST_CS);
  161. #elif defined (LDAP_X_OPT_CONNECT_TIMEOUT)
  162. REGISTER_LONG_CONSTANT("LDAP_OPT_NETWORK_TIMEOUT", LDAP_X_OPT_CONNECT_TIMEOUT, CONST_PERSISTENT | CONST_CS);
  163. #endif
  164. #ifdef LDAP_OPT_TIMEOUT
  165. REGISTER_LONG_CONSTANT("LDAP_OPT_TIMEOUT", LDAP_OPT_TIMEOUT, CONST_PERSISTENT | CONST_CS);
  166. #endif
  167. REGISTER_LONG_CONSTANT("LDAP_OPT_PROTOCOL_VERSION", LDAP_OPT_PROTOCOL_VERSION, CONST_PERSISTENT | CONST_CS);
  168. REGISTER_LONG_CONSTANT("LDAP_OPT_ERROR_NUMBER", LDAP_OPT_ERROR_NUMBER, CONST_PERSISTENT | CONST_CS);
  169. REGISTER_LONG_CONSTANT("LDAP_OPT_REFERRALS", LDAP_OPT_REFERRALS, CONST_PERSISTENT | CONST_CS);
  170. #ifdef LDAP_OPT_RESTART
  171. REGISTER_LONG_CONSTANT("LDAP_OPT_RESTART", LDAP_OPT_RESTART, CONST_PERSISTENT | CONST_CS);
  172. #endif
  173. #ifdef LDAP_OPT_HOST_NAME
  174. REGISTER_LONG_CONSTANT("LDAP_OPT_HOST_NAME", LDAP_OPT_HOST_NAME, CONST_PERSISTENT | CONST_CS);
  175. #endif
  176. REGISTER_LONG_CONSTANT("LDAP_OPT_ERROR_STRING", LDAP_OPT_ERROR_STRING, CONST_PERSISTENT | CONST_CS);
  177. #ifdef LDAP_OPT_MATCHED_DN
  178. REGISTER_LONG_CONSTANT("LDAP_OPT_MATCHED_DN", LDAP_OPT_MATCHED_DN, CONST_PERSISTENT | CONST_CS);
  179. #endif
  180. REGISTER_LONG_CONSTANT("LDAP_OPT_SERVER_CONTROLS", LDAP_OPT_SERVER_CONTROLS, CONST_PERSISTENT | CONST_CS);
  181. REGISTER_LONG_CONSTANT("LDAP_OPT_CLIENT_CONTROLS", LDAP_OPT_CLIENT_CONTROLS, CONST_PERSISTENT | CONST_CS);
  182. #endif
  183. #ifdef LDAP_OPT_DEBUG_LEVEL
  184. REGISTER_LONG_CONSTANT("LDAP_OPT_DEBUG_LEVEL", LDAP_OPT_DEBUG_LEVEL, CONST_PERSISTENT | CONST_CS);
  185. #endif
  186. #ifdef LDAP_OPT_DIAGNOSTIC_MESSAGE
  187. REGISTER_LONG_CONSTANT("LDAP_OPT_DIAGNOSTIC_MESSAGE", LDAP_OPT_DIAGNOSTIC_MESSAGE, CONST_PERSISTENT | CONST_CS);
  188. #endif
  189. #ifdef HAVE_LDAP_SASL
  190. REGISTER_LONG_CONSTANT("LDAP_OPT_X_SASL_MECH", LDAP_OPT_X_SASL_MECH, CONST_PERSISTENT | CONST_CS);
  191. REGISTER_LONG_CONSTANT("LDAP_OPT_X_SASL_REALM", LDAP_OPT_X_SASL_REALM, CONST_PERSISTENT | CONST_CS);
  192. REGISTER_LONG_CONSTANT("LDAP_OPT_X_SASL_AUTHCID", LDAP_OPT_X_SASL_AUTHCID, CONST_PERSISTENT | CONST_CS);
  193. REGISTER_LONG_CONSTANT("LDAP_OPT_X_SASL_AUTHZID", LDAP_OPT_X_SASL_AUTHZID, CONST_PERSISTENT | CONST_CS);
  194. #endif
  195. #ifdef ORALDAP
  196. REGISTER_LONG_CONSTANT("GSLC_SSL_NO_AUTH", GSLC_SSL_NO_AUTH, CONST_PERSISTENT | CONST_CS);
  197. REGISTER_LONG_CONSTANT("GSLC_SSL_ONEWAY_AUTH", GSLC_SSL_ONEWAY_AUTH, CONST_PERSISTENT | CONST_CS);
  198. REGISTER_LONG_CONSTANT("GSLC_SSL_TWOWAY_AUTH", GSLC_SSL_TWOWAY_AUTH, CONST_PERSISTENT | CONST_CS);
  199. #endif
  200. REGISTER_LONG_CONSTANT("LDAP_ESCAPE_FILTER", PHP_LDAP_ESCAPE_FILTER, CONST_PERSISTENT | CONST_CS);
  201. REGISTER_LONG_CONSTANT("LDAP_ESCAPE_DN", PHP_LDAP_ESCAPE_DN, CONST_PERSISTENT | CONST_CS);
  202. le_link = zend_register_list_destructors_ex(_close_ldap_link, NULL, "ldap link", module_number);
  203. le_result = zend_register_list_destructors_ex(_free_ldap_result, NULL, "ldap result", module_number);
  204. le_result_entry = zend_register_list_destructors_ex(_free_ldap_result_entry, NULL, "ldap result entry", module_number);
  205. Z_TYPE(ldap_module_entry) = type;
  206. return SUCCESS;
  207. }
  208. /* }}} */
  209. /* {{{ PHP_MSHUTDOWN_FUNCTION
  210. */
  211. PHP_MSHUTDOWN_FUNCTION(ldap)
  212. {
  213. UNREGISTER_INI_ENTRIES();
  214. return SUCCESS;
  215. }
  216. /* }}} */
  217. /* {{{ PHP_MINFO_FUNCTION
  218. */
  219. PHP_MINFO_FUNCTION(ldap)
  220. {
  221. char tmp[32];
  222. #if HAVE_NSLDAP
  223. LDAPVersion ver;
  224. double SDKVersion;
  225. #endif
  226. php_info_print_table_start();
  227. php_info_print_table_row(2, "LDAP Support", "enabled");
  228. php_info_print_table_row(2, "RCS Version", "$Id$");
  229. if (LDAPG(max_links) == -1) {
  230. snprintf(tmp, 31, "%ld/unlimited", LDAPG(num_links));
  231. } else {
  232. snprintf(tmp, 31, "%ld/%ld", LDAPG(num_links), LDAPG(max_links));
  233. }
  234. php_info_print_table_row(2, "Total Links", tmp);
  235. #ifdef LDAP_API_VERSION
  236. snprintf(tmp, 31, "%d", LDAP_API_VERSION);
  237. php_info_print_table_row(2, "API Version", tmp);
  238. #endif
  239. #ifdef LDAP_VENDOR_NAME
  240. php_info_print_table_row(2, "Vendor Name", LDAP_VENDOR_NAME);
  241. #endif
  242. #ifdef LDAP_VENDOR_VERSION
  243. snprintf(tmp, 31, "%d", LDAP_VENDOR_VERSION);
  244. php_info_print_table_row(2, "Vendor Version", tmp);
  245. #endif
  246. #if HAVE_NSLDAP
  247. SDKVersion = ldap_version(&ver);
  248. snprintf(tmp, 31, "%F", SDKVersion/100.0);
  249. php_info_print_table_row(2, "SDK Version", tmp);
  250. snprintf(tmp, 31, "%F", ver.protocol_version/100.0);
  251. php_info_print_table_row(2, "Highest LDAP Protocol Supported", tmp);
  252. snprintf(tmp, 31, "%F", ver.SSL_version/100.0);
  253. php_info_print_table_row(2, "SSL Level Supported", tmp);
  254. if (ver.security_level != LDAP_SECURITY_NONE) {
  255. snprintf(tmp, 31, "%d", ver.security_level);
  256. } else {
  257. strcpy(tmp, "SSL not enabled");
  258. }
  259. php_info_print_table_row(2, "Level of Encryption", tmp);
  260. #endif
  261. #ifdef HAVE_LDAP_SASL
  262. php_info_print_table_row(2, "SASL Support", "Enabled");
  263. #endif
  264. php_info_print_table_end();
  265. DISPLAY_INI_ENTRIES();
  266. }
  267. /* }}} */
  268. /* {{{ proto resource ldap_connect([string host [, int port [, string wallet [, string wallet_passwd [, int authmode]]]]])
  269. Connect to an LDAP server */
  270. PHP_FUNCTION(ldap_connect)
  271. {
  272. char *host = NULL;
  273. int hostlen;
  274. long port = LDAP_PORT;
  275. #ifdef HAVE_ORALDAP
  276. char *wallet = NULL, *walletpasswd = NULL;
  277. int walletlen = 0, walletpasswdlen = 0;
  278. long authmode = GSLC_SSL_NO_AUTH;
  279. int ssl=0;
  280. #endif
  281. ldap_linkdata *ld;
  282. LDAP *ldap = NULL;
  283. #ifdef HAVE_ORALDAP
  284. if (ZEND_NUM_ARGS() == 3 || ZEND_NUM_ARGS() == 4) {
  285. WRONG_PARAM_COUNT;
  286. }
  287. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|slssl", &host, &hostlen, &port, &wallet, &walletlen, &walletpasswd, &walletpasswdlen, &authmode) != SUCCESS) {
  288. RETURN_FALSE;
  289. }
  290. if (ZEND_NUM_ARGS() == 5) {
  291. ssl = 1;
  292. }
  293. #else
  294. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &host, &hostlen, &port) != SUCCESS) {
  295. RETURN_FALSE;
  296. }
  297. #endif
  298. if (!port) {
  299. port = LDAP_PORT;
  300. }
  301. if (LDAPG(max_links) != -1 && LDAPG(num_links) >= LDAPG(max_links)) {
  302. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%ld)", LDAPG(num_links));
  303. RETURN_FALSE;
  304. }
  305. ld = ecalloc(1, sizeof(ldap_linkdata));
  306. {
  307. int rc = LDAP_SUCCESS;
  308. char *url = host;
  309. if (!ldap_is_ldap_url(url)) {
  310. int urllen = hostlen + sizeof( "ldap://:65535" );
  311. if (port <= 0 || port > 65535) {
  312. efree(ld);
  313. php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid port number: %ld", port);
  314. RETURN_FALSE;
  315. }
  316. url = emalloc(urllen);
  317. if (host && (strchr(host, ':') != NULL)) {
  318. /* Legacy support for host:port */
  319. snprintf( url, urllen, "ldap://%s", host );
  320. } else {
  321. snprintf( url, urllen, "ldap://%s:%ld", host ? host : "", port );
  322. }
  323. }
  324. #ifdef LDAP_API_FEATURE_X_OPENLDAP
  325. /* ldap_init() is deprecated, use ldap_initialize() instead.
  326. */
  327. rc = ldap_initialize(&ldap, url);
  328. #else /* ! LDAP_API_FEATURE_X_OPENLDAP */
  329. /* ldap_init does not support URLs.
  330. * We must try the original host and port information.
  331. */
  332. ldap = ldap_init(host, port);
  333. if (ldap == NULL) {
  334. efree(ld);
  335. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not create session handle");
  336. RETURN_FALSE;
  337. }
  338. #endif /* ! LDAP_API_FEATURE_X_OPENLDAP */
  339. if (url != host) {
  340. efree(url);
  341. }
  342. if (rc != LDAP_SUCCESS) {
  343. efree(ld);
  344. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not create session handle: %s", ldap_err2string(rc));
  345. RETURN_FALSE;
  346. }
  347. }
  348. if (ldap == NULL) {
  349. efree(ld);
  350. RETURN_FALSE;
  351. } else {
  352. #ifdef HAVE_ORALDAP
  353. if (ssl) {
  354. if (ldap_init_SSL(&ldap->ld_sb, wallet, walletpasswd, authmode)) {
  355. efree(ld);
  356. php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSL init failed");
  357. RETURN_FALSE;
  358. }
  359. }
  360. #endif
  361. LDAPG(num_links)++;
  362. ld->link = ldap;
  363. ZEND_REGISTER_RESOURCE(return_value, ld, le_link);
  364. }
  365. }
  366. /* }}} */
  367. /* {{{ _get_lderrno
  368. */
  369. static int _get_lderrno(LDAP *ldap)
  370. {
  371. #if !HAVE_NSLDAP
  372. #if LDAP_API_VERSION > 2000 || HAVE_ORALDAP
  373. int lderr;
  374. /* New versions of OpenLDAP do it this way */
  375. ldap_get_option(ldap, LDAP_OPT_ERROR_NUMBER, &lderr);
  376. return lderr;
  377. #else
  378. return ldap->ld_errno;
  379. #endif
  380. #else
  381. return ldap_get_lderrno(ldap, NULL, NULL);
  382. #endif
  383. }
  384. /* }}} */
  385. /* {{{ _set_lderrno
  386. */
  387. static void _set_lderrno(LDAP *ldap, int lderr)
  388. {
  389. #if !HAVE_NSLDAP
  390. #if LDAP_API_VERSION > 2000 || HAVE_ORALDAP
  391. /* New versions of OpenLDAP do it this way */
  392. ldap_set_option(ldap, LDAP_OPT_ERROR_NUMBER, &lderr);
  393. #else
  394. ldap->ld_errno = lderr;
  395. #endif
  396. #else
  397. ldap_set_lderrno(ldap, lderr, NULL, NULL);
  398. #endif
  399. }
  400. /* }}} */
  401. /* {{{ proto bool ldap_bind(resource link [, string dn [, string password]])
  402. Bind to LDAP directory */
  403. PHP_FUNCTION(ldap_bind)
  404. {
  405. zval *link;
  406. char *ldap_bind_dn = NULL, *ldap_bind_pw = NULL;
  407. int ldap_bind_dnlen, ldap_bind_pwlen;
  408. ldap_linkdata *ld;
  409. int rc;
  410. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ss", &link, &ldap_bind_dn, &ldap_bind_dnlen, &ldap_bind_pw, &ldap_bind_pwlen) != SUCCESS) {
  411. RETURN_FALSE;
  412. }
  413. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  414. if (ldap_bind_dn != NULL && memchr(ldap_bind_dn, '\0', ldap_bind_dnlen) != NULL) {
  415. _set_lderrno(ld->link, LDAP_INVALID_CREDENTIALS);
  416. php_error_docref(NULL TSRMLS_CC, E_WARNING, "DN contains a null byte");
  417. RETURN_FALSE;
  418. }
  419. if (ldap_bind_pw != NULL && memchr(ldap_bind_pw, '\0', ldap_bind_pwlen) != NULL) {
  420. _set_lderrno(ld->link, LDAP_INVALID_CREDENTIALS);
  421. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Password contains a null byte");
  422. RETURN_FALSE;
  423. }
  424. {
  425. #ifdef LDAP_API_FEATURE_X_OPENLDAP
  426. /* ldap_simple_bind_s() is deprecated, use ldap_sasl_bind_s() instead.
  427. */
  428. struct berval cred;
  429. cred.bv_val = ldap_bind_pw;
  430. cred.bv_len = ldap_bind_pw ? ldap_bind_pwlen : 0;
  431. rc = ldap_sasl_bind_s(ld->link, ldap_bind_dn, LDAP_SASL_SIMPLE, &cred,
  432. NULL, NULL, /* no controls right now */
  433. NULL); /* we don't care about the server's credentials */
  434. #else /* ! LDAP_API_FEATURE_X_OPENLDAP */
  435. rc = ldap_simple_bind_s(ld->link, ldap_bind_dn, ldap_bind_pw);
  436. #endif /* ! LDAP_API_FEATURE_X_OPENLDAP */
  437. }
  438. if ( rc != LDAP_SUCCESS) {
  439. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to bind to server: %s", ldap_err2string(rc));
  440. RETURN_FALSE;
  441. } else {
  442. RETURN_TRUE;
  443. }
  444. }
  445. /* }}} */
  446. #ifdef HAVE_LDAP_SASL
  447. typedef struct {
  448. char *mech;
  449. char *realm;
  450. char *authcid;
  451. char *passwd;
  452. char *authzid;
  453. } php_ldap_bictx;
  454. /* {{{ _php_sasl_setdefs
  455. */
  456. static php_ldap_bictx *_php_sasl_setdefs(LDAP *ld, char *sasl_mech, char *sasl_realm, char *sasl_authc_id, char *passwd, char *sasl_authz_id)
  457. {
  458. php_ldap_bictx *ctx;
  459. ctx = ber_memalloc(sizeof(php_ldap_bictx));
  460. ctx->mech = (sasl_mech) ? ber_strdup(sasl_mech) : NULL;
  461. ctx->realm = (sasl_realm) ? ber_strdup(sasl_realm) : NULL;
  462. ctx->authcid = (sasl_authc_id) ? ber_strdup(sasl_authc_id) : NULL;
  463. ctx->passwd = (passwd) ? ber_strdup(passwd) : NULL;
  464. ctx->authzid = (sasl_authz_id) ? ber_strdup(sasl_authz_id) : NULL;
  465. if (ctx->mech == NULL) {
  466. ldap_get_option(ld, LDAP_OPT_X_SASL_MECH, &ctx->mech);
  467. }
  468. if (ctx->realm == NULL) {
  469. ldap_get_option(ld, LDAP_OPT_X_SASL_REALM, &ctx->realm);
  470. }
  471. if (ctx->authcid == NULL) {
  472. ldap_get_option(ld, LDAP_OPT_X_SASL_AUTHCID, &ctx->authcid);
  473. }
  474. if (ctx->authzid == NULL) {
  475. ldap_get_option(ld, LDAP_OPT_X_SASL_AUTHZID, &ctx->authzid);
  476. }
  477. return ctx;
  478. }
  479. /* }}} */
  480. /* {{{ _php_sasl_freedefs
  481. */
  482. static void _php_sasl_freedefs(php_ldap_bictx *ctx)
  483. {
  484. if (ctx->mech) ber_memfree(ctx->mech);
  485. if (ctx->realm) ber_memfree(ctx->realm);
  486. if (ctx->authcid) ber_memfree(ctx->authcid);
  487. if (ctx->passwd) ber_memfree(ctx->passwd);
  488. if (ctx->authzid) ber_memfree(ctx->authzid);
  489. ber_memfree(ctx);
  490. }
  491. /* }}} */
  492. /* {{{ _php_sasl_interact
  493. Internal interact function for SASL */
  494. static int _php_sasl_interact(LDAP *ld, unsigned flags, void *defaults, void *in)
  495. {
  496. sasl_interact_t *interact = in;
  497. const char *p;
  498. php_ldap_bictx *ctx = defaults;
  499. for (;interact->id != SASL_CB_LIST_END;interact++) {
  500. p = NULL;
  501. switch(interact->id) {
  502. case SASL_CB_GETREALM:
  503. p = ctx->realm;
  504. break;
  505. case SASL_CB_AUTHNAME:
  506. p = ctx->authcid;
  507. break;
  508. case SASL_CB_USER:
  509. p = ctx->authzid;
  510. break;
  511. case SASL_CB_PASS:
  512. p = ctx->passwd;
  513. break;
  514. }
  515. if (p) {
  516. interact->result = p;
  517. interact->len = strlen(interact->result);
  518. }
  519. }
  520. return LDAP_SUCCESS;
  521. }
  522. /* }}} */
  523. /* {{{ proto bool ldap_sasl_bind(resource link [, string binddn [, string password [, string sasl_mech [, string sasl_realm [, string sasl_authc_id [, string sasl_authz_id [, string props]]]]]]])
  524. Bind to LDAP directory using SASL */
  525. PHP_FUNCTION(ldap_sasl_bind)
  526. {
  527. zval *link;
  528. ldap_linkdata *ld;
  529. char *binddn = NULL;
  530. char *passwd = NULL;
  531. char *sasl_mech = NULL;
  532. char *sasl_realm = NULL;
  533. char *sasl_authz_id = NULL;
  534. char *sasl_authc_id = NULL;
  535. char *props = NULL;
  536. int rc, dn_len, passwd_len, mech_len, realm_len, authc_id_len, authz_id_len, props_len;
  537. php_ldap_bictx *ctx;
  538. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|sssssss", &link, &binddn, &dn_len, &passwd, &passwd_len, &sasl_mech, &mech_len, &sasl_realm, &realm_len, &sasl_authc_id, &authc_id_len, &sasl_authz_id, &authz_id_len, &props, &props_len) != SUCCESS) {
  539. RETURN_FALSE;
  540. }
  541. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  542. ctx = _php_sasl_setdefs(ld->link, sasl_mech, sasl_realm, sasl_authc_id, passwd, sasl_authz_id);
  543. if (props) {
  544. ldap_set_option(ld->link, LDAP_OPT_X_SASL_SECPROPS, props);
  545. }
  546. rc = ldap_sasl_interactive_bind_s(ld->link, binddn, ctx->mech, NULL, NULL, LDAP_SASL_QUIET, _php_sasl_interact, ctx);
  547. if (rc != LDAP_SUCCESS) {
  548. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to bind to server: %s", ldap_err2string(rc));
  549. RETVAL_FALSE;
  550. } else {
  551. RETVAL_TRUE;
  552. }
  553. _php_sasl_freedefs(ctx);
  554. }
  555. /* }}} */
  556. #endif /* HAVE_LDAP_SASL */
  557. /* {{{ proto bool ldap_unbind(resource link)
  558. Unbind from LDAP directory */
  559. PHP_FUNCTION(ldap_unbind)
  560. {
  561. zval *link;
  562. ldap_linkdata *ld;
  563. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &link) != SUCCESS) {
  564. RETURN_FALSE;
  565. }
  566. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  567. zend_list_delete(Z_LVAL_P(link));
  568. RETURN_TRUE;
  569. }
  570. /* }}} */
  571. /* {{{ php_set_opts
  572. */
  573. static void php_set_opts(LDAP *ldap, int sizelimit, int timelimit, int deref, int *old_sizelimit, int *old_timelimit, int *old_deref)
  574. {
  575. /* sizelimit */
  576. if (sizelimit > -1) {
  577. #if (LDAP_API_VERSION >= 2004) || HAVE_NSLDAP || HAVE_ORALDAP
  578. ldap_get_option(ldap, LDAP_OPT_SIZELIMIT, old_sizelimit);
  579. ldap_set_option(ldap, LDAP_OPT_SIZELIMIT, &sizelimit);
  580. #else
  581. *old_sizelimit = ldap->ld_sizelimit;
  582. ldap->ld_sizelimit = sizelimit;
  583. #endif
  584. }
  585. /* timelimit */
  586. if (timelimit > -1) {
  587. #if (LDAP_API_VERSION >= 2004) || HAVE_NSLDAP || HAVE_ORALDAP
  588. ldap_get_option(ldap, LDAP_OPT_TIMELIMIT, old_timelimit);
  589. ldap_set_option(ldap, LDAP_OPT_TIMELIMIT, &timelimit);
  590. #else
  591. *old_timelimit = ldap->ld_timelimit;
  592. ldap->ld_timelimit = timelimit;
  593. #endif
  594. }
  595. /* deref */
  596. if (deref > -1) {
  597. #if (LDAP_API_VERSION >= 2004) || HAVE_NSLDAP || HAVE_ORALDAP
  598. ldap_get_option(ldap, LDAP_OPT_DEREF, old_deref);
  599. ldap_set_option(ldap, LDAP_OPT_DEREF, &deref);
  600. #else
  601. *old_deref = ldap->ld_deref;
  602. ldap->ld_deref = deref;
  603. #endif
  604. }
  605. }
  606. /* }}} */
  607. /* {{{ php_ldap_do_search
  608. */
  609. static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
  610. {
  611. zval *link, *base_dn, **filter, *attrs = NULL, **attr;
  612. long attrsonly, sizelimit, timelimit, deref;
  613. char *ldap_base_dn = NULL, *ldap_filter = NULL, **ldap_attrs = NULL;
  614. ldap_linkdata *ld = NULL;
  615. LDAPMessage *ldap_res;
  616. int ldap_attrsonly = 0, ldap_sizelimit = -1, ldap_timelimit = -1, ldap_deref = -1;
  617. int old_ldap_sizelimit = -1, old_ldap_timelimit = -1, old_ldap_deref = -1;
  618. int num_attribs = 0, ret = 1, i, errno, argcount = ZEND_NUM_ARGS();
  619. if (zend_parse_parameters(argcount TSRMLS_CC, "zzZ|allll", &link, &base_dn, &filter, &attrs, &attrsonly,
  620. &sizelimit, &timelimit, &deref) == FAILURE) {
  621. return;
  622. }
  623. /* Reverse -> fall through */
  624. switch (argcount) {
  625. case 8:
  626. ldap_deref = deref;
  627. case 7:
  628. ldap_timelimit = timelimit;
  629. case 6:
  630. ldap_sizelimit = sizelimit;
  631. case 5:
  632. ldap_attrsonly = attrsonly;
  633. case 4:
  634. num_attribs = zend_hash_num_elements(Z_ARRVAL_P(attrs));
  635. ldap_attrs = safe_emalloc((num_attribs+1), sizeof(char *), 0);
  636. for (i = 0; i<num_attribs; i++) {
  637. if (zend_hash_index_find(Z_ARRVAL_P(attrs), i, (void **) &attr) != SUCCESS) {
  638. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array initialization wrong");
  639. ret = 0;
  640. goto cleanup;
  641. }
  642. SEPARATE_ZVAL(attr);
  643. convert_to_string_ex(attr);
  644. ldap_attrs[i] = Z_STRVAL_PP(attr);
  645. }
  646. ldap_attrs[num_attribs] = NULL;
  647. default:
  648. break;
  649. }
  650. /* parallel search? */
  651. if (Z_TYPE_P(link) == IS_ARRAY) {
  652. int i, nlinks, nbases, nfilters, *rcs;
  653. ldap_linkdata **lds;
  654. zval **entry, *resource;
  655. nlinks = zend_hash_num_elements(Z_ARRVAL_P(link));
  656. if (nlinks == 0) {
  657. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No links in link array");
  658. ret = 0;
  659. goto cleanup;
  660. }
  661. if (Z_TYPE_P(base_dn) == IS_ARRAY) {
  662. nbases = zend_hash_num_elements(Z_ARRVAL_P(base_dn));
  663. if (nbases != nlinks) {
  664. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Base must either be a string, or an array with the same number of elements as the links array");
  665. ret = 0;
  666. goto cleanup;
  667. }
  668. zend_hash_internal_pointer_reset(Z_ARRVAL_P(base_dn));
  669. } else {
  670. nbases = 0; /* this means string, not array */
  671. /* If anything else than string is passed, ldap_base_dn = NULL */
  672. if (Z_TYPE_P(base_dn) == IS_STRING) {
  673. ldap_base_dn = Z_STRVAL_P(base_dn);
  674. } else {
  675. ldap_base_dn = NULL;
  676. }
  677. }
  678. if (Z_TYPE_PP(filter) == IS_ARRAY) {
  679. nfilters = zend_hash_num_elements(Z_ARRVAL_PP(filter));
  680. if (nfilters != nlinks) {
  681. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Filter must either be a string, or an array with the same number of elements as the links array");
  682. ret = 0;
  683. goto cleanup;
  684. }
  685. zend_hash_internal_pointer_reset(Z_ARRVAL_PP(filter));
  686. } else {
  687. nfilters = 0; /* this means string, not array */
  688. convert_to_string_ex(filter);
  689. ldap_filter = Z_STRVAL_PP(filter);
  690. }
  691. lds = safe_emalloc(nlinks, sizeof(ldap_linkdata), 0);
  692. rcs = safe_emalloc(nlinks, sizeof(*rcs), 0);
  693. zend_hash_internal_pointer_reset(Z_ARRVAL_P(link));
  694. for (i=0; i<nlinks; i++) {
  695. zend_hash_get_current_data(Z_ARRVAL_P(link), (void **)&entry);
  696. ld = (ldap_linkdata *) zend_fetch_resource(entry TSRMLS_CC, -1, "ldap link", NULL, 1, le_link);
  697. if (ld == NULL) {
  698. ret = 0;
  699. goto cleanup_parallel;
  700. }
  701. if (nbases != 0) { /* base_dn an array? */
  702. zend_hash_get_current_data(Z_ARRVAL_P(base_dn), (void **)&entry);
  703. zend_hash_move_forward(Z_ARRVAL_P(base_dn));
  704. /* If anything else than string is passed, ldap_base_dn = NULL */
  705. if (Z_TYPE_PP(entry) == IS_STRING) {
  706. ldap_base_dn = Z_STRVAL_PP(entry);
  707. } else {
  708. ldap_base_dn = NULL;
  709. }
  710. }
  711. if (nfilters != 0) { /* filter an array? */
  712. zend_hash_get_current_data(Z_ARRVAL_PP(filter), (void **)&entry);
  713. zend_hash_move_forward(Z_ARRVAL_PP(filter));
  714. convert_to_string_ex(entry);
  715. ldap_filter = Z_STRVAL_PP(entry);
  716. }
  717. php_set_opts(ld->link, ldap_sizelimit, ldap_timelimit, ldap_deref, &old_ldap_sizelimit, &old_ldap_timelimit, &old_ldap_deref);
  718. /* Run the actual search */
  719. ldap_search_ext(ld->link, ldap_base_dn, scope, ldap_filter, ldap_attrs, ldap_attrsonly, NULL, NULL, NULL, ldap_sizelimit, &rcs[i]);
  720. lds[i] = ld;
  721. zend_hash_move_forward(Z_ARRVAL_P(link));
  722. }
  723. array_init(return_value);
  724. /* Collect results from the searches */
  725. for (i=0; i<nlinks; i++) {
  726. MAKE_STD_ZVAL(resource);
  727. if (rcs[i] != -1) {
  728. rcs[i] = ldap_result(lds[i]->link, LDAP_RES_ANY, 1 /* LDAP_MSG_ALL */, NULL, &ldap_res);
  729. }
  730. if (rcs[i] != -1) {
  731. ZEND_REGISTER_RESOURCE(resource, ldap_res, le_result);
  732. add_next_index_zval(return_value, resource);
  733. } else {
  734. add_next_index_bool(return_value, 0);
  735. }
  736. }
  737. cleanup_parallel:
  738. efree(lds);
  739. efree(rcs);
  740. } else {
  741. convert_to_string_ex(filter);
  742. ldap_filter = Z_STRVAL_PP(filter);
  743. /* If anything else than string is passed, ldap_base_dn = NULL */
  744. if (Z_TYPE_P(base_dn) == IS_STRING) {
  745. ldap_base_dn = Z_STRVAL_P(base_dn);
  746. }
  747. ld = (ldap_linkdata *) zend_fetch_resource(&link TSRMLS_CC, -1, "ldap link", NULL, 1, le_link);
  748. if (ld == NULL) {
  749. ret = 0;
  750. goto cleanup;
  751. }
  752. php_set_opts(ld->link, ldap_sizelimit, ldap_timelimit, ldap_deref, &old_ldap_sizelimit, &old_ldap_timelimit, &old_ldap_deref);
  753. /* Run the actual search */
  754. errno = ldap_search_ext_s(ld->link, ldap_base_dn, scope, ldap_filter, ldap_attrs, ldap_attrsonly, NULL, NULL, NULL, ldap_sizelimit, &ldap_res);
  755. if (errno != LDAP_SUCCESS
  756. && errno != LDAP_SIZELIMIT_EXCEEDED
  757. #ifdef LDAP_ADMINLIMIT_EXCEEDED
  758. && errno != LDAP_ADMINLIMIT_EXCEEDED
  759. #endif
  760. #ifdef LDAP_REFERRAL
  761. && errno != LDAP_REFERRAL
  762. #endif
  763. ) {
  764. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Search: %s", ldap_err2string(errno));
  765. ret = 0;
  766. } else {
  767. if (errno == LDAP_SIZELIMIT_EXCEEDED) {
  768. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Partial search results returned: Sizelimit exceeded");
  769. }
  770. #ifdef LDAP_ADMINLIMIT_EXCEEDED
  771. else if (errno == LDAP_ADMINLIMIT_EXCEEDED) {
  772. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Partial search results returned: Adminlimit exceeded");
  773. }
  774. #endif
  775. ZEND_REGISTER_RESOURCE(return_value, ldap_res, le_result);
  776. }
  777. }
  778. cleanup:
  779. if (ld) {
  780. /* Restoring previous options */
  781. php_set_opts(ld->link, old_ldap_sizelimit, old_ldap_timelimit, old_ldap_deref, &ldap_sizelimit, &ldap_timelimit, &ldap_deref);
  782. }
  783. if (ldap_attrs != NULL) {
  784. efree(ldap_attrs);
  785. }
  786. if (!ret) {
  787. RETVAL_BOOL(ret);
  788. }
  789. }
  790. /* }}} */
  791. /* {{{ proto resource ldap_read(resource|array link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
  792. Read an entry */
  793. PHP_FUNCTION(ldap_read)
  794. {
  795. php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_BASE);
  796. }
  797. /* }}} */
  798. /* {{{ proto resource ldap_list(resource|array link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
  799. Single-level search */
  800. PHP_FUNCTION(ldap_list)
  801. {
  802. php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_ONELEVEL);
  803. }
  804. /* }}} */
  805. /* {{{ proto resource ldap_search(resource|array link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]])
  806. Search LDAP tree under base_dn */
  807. PHP_FUNCTION(ldap_search)
  808. {
  809. php_ldap_do_search(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_SCOPE_SUBTREE);
  810. }
  811. /* }}} */
  812. /* {{{ proto bool ldap_free_result(resource result)
  813. Free result memory */
  814. PHP_FUNCTION(ldap_free_result)
  815. {
  816. zval *result;
  817. LDAPMessage *ldap_result;
  818. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) != SUCCESS) {
  819. return;
  820. }
  821. ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
  822. zend_list_delete(Z_LVAL_P(result)); /* Delete list entry */
  823. RETVAL_TRUE;
  824. }
  825. /* }}} */
  826. /* {{{ proto int ldap_count_entries(resource link, resource result)
  827. Count the number of entries in a search result */
  828. PHP_FUNCTION(ldap_count_entries)
  829. {
  830. zval *link, *result;
  831. ldap_linkdata *ld;
  832. LDAPMessage *ldap_result;
  833. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result) != SUCCESS) {
  834. return;
  835. }
  836. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  837. ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
  838. RETURN_LONG(ldap_count_entries(ld->link, ldap_result));
  839. }
  840. /* }}} */
  841. /* {{{ proto resource ldap_first_entry(resource link, resource result)
  842. Return first result id */
  843. PHP_FUNCTION(ldap_first_entry)
  844. {
  845. zval *link, *result;
  846. ldap_linkdata *ld;
  847. ldap_resultentry *resultentry;
  848. LDAPMessage *ldap_result, *entry;
  849. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result) != SUCCESS) {
  850. return;
  851. }
  852. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  853. ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
  854. if ((entry = ldap_first_entry(ld->link, ldap_result)) == NULL) {
  855. RETVAL_FALSE;
  856. } else {
  857. resultentry = emalloc(sizeof(ldap_resultentry));
  858. ZEND_REGISTER_RESOURCE(return_value, resultentry, le_result_entry);
  859. resultentry->id = Z_LVAL_P(result);
  860. zend_list_addref(resultentry->id);
  861. resultentry->data = entry;
  862. resultentry->ber = NULL;
  863. }
  864. }
  865. /* }}} */
  866. /* {{{ proto resource ldap_next_entry(resource link, resource result_entry)
  867. Get next result entry */
  868. PHP_FUNCTION(ldap_next_entry)
  869. {
  870. zval *link, *result_entry;
  871. ldap_linkdata *ld;
  872. ldap_resultentry *resultentry, *resultentry_next;
  873. LDAPMessage *entry_next;
  874. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result_entry) != SUCCESS) {
  875. return;
  876. }
  877. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  878. ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
  879. if ((entry_next = ldap_next_entry(ld->link, resultentry->data)) == NULL) {
  880. RETVAL_FALSE;
  881. } else {
  882. resultentry_next = emalloc(sizeof(ldap_resultentry));
  883. ZEND_REGISTER_RESOURCE(return_value, resultentry_next, le_result_entry);
  884. resultentry_next->id = resultentry->id;
  885. zend_list_addref(resultentry->id);
  886. resultentry_next->data = entry_next;
  887. resultentry_next->ber = NULL;
  888. }
  889. }
  890. /* }}} */
  891. /* {{{ proto array ldap_get_entries(resource link, resource result)
  892. Get all result entries */
  893. PHP_FUNCTION(ldap_get_entries)
  894. {
  895. zval *link, *result;
  896. LDAPMessage *ldap_result, *ldap_result_entry;
  897. zval *tmp1, *tmp2;
  898. ldap_linkdata *ld;
  899. LDAP *ldap;
  900. int num_entries, num_attrib, num_values, i;
  901. BerElement *ber;
  902. char *attribute;
  903. size_t attr_len;
  904. struct berval **ldap_value;
  905. char *dn;
  906. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result) != SUCCESS) {
  907. return;
  908. }
  909. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  910. ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
  911. ldap = ld->link;
  912. num_entries = ldap_count_entries(ldap, ldap_result);
  913. array_init(return_value);
  914. add_assoc_long(return_value, "count", num_entries);
  915. if (num_entries == 0) {
  916. return;
  917. }
  918. ldap_result_entry = ldap_first_entry(ldap, ldap_result);
  919. if (ldap_result_entry == NULL) {
  920. zval_dtor(return_value);
  921. RETURN_FALSE;
  922. }
  923. num_entries = 0;
  924. while (ldap_result_entry != NULL) {
  925. MAKE_STD_ZVAL(tmp1);
  926. array_init(tmp1);
  927. num_attrib = 0;
  928. attribute = ldap_first_attribute(ldap, ldap_result_entry, &ber);
  929. while (attribute != NULL) {
  930. ldap_value = ldap_get_values_len(ldap, ldap_result_entry, attribute);
  931. num_values = ldap_count_values_len(ldap_value);
  932. MAKE_STD_ZVAL(tmp2);
  933. array_init(tmp2);
  934. add_assoc_long(tmp2, "count", num_values);
  935. for (i = 0; i < num_values; i++) {
  936. add_index_stringl(tmp2, i, ldap_value[i]->bv_val, ldap_value[i]->bv_len, 1);
  937. }
  938. ldap_value_free_len(ldap_value);
  939. attr_len = strlen(attribute);
  940. zend_hash_update(Z_ARRVAL_P(tmp1), php_strtolower(attribute, attr_len), attr_len+1, (void *) &tmp2, sizeof(zval *), NULL);
  941. add_index_string(tmp1, num_attrib, attribute, 1);
  942. num_attrib++;
  943. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
  944. ldap_memfree(attribute);
  945. #endif
  946. attribute = ldap_next_attribute(ldap, ldap_result_entry, ber);
  947. }
  948. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
  949. if (ber != NULL) {
  950. ber_free(ber, 0);
  951. }
  952. #endif
  953. add_assoc_long(tmp1, "count", num_attrib);
  954. dn = ldap_get_dn(ldap, ldap_result_entry);
  955. if (dn) {
  956. add_assoc_string(tmp1, "dn", dn, 1);
  957. } else {
  958. add_assoc_null(tmp1, "dn");
  959. }
  960. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
  961. ldap_memfree(dn);
  962. #else
  963. free(dn);
  964. #endif
  965. zend_hash_index_update(Z_ARRVAL_P(return_value), num_entries, (void *) &tmp1, sizeof(zval *), NULL);
  966. num_entries++;
  967. ldap_result_entry = ldap_next_entry(ldap, ldap_result_entry);
  968. }
  969. add_assoc_long(return_value, "count", num_entries);
  970. }
  971. /* }}} */
  972. /* {{{ proto string ldap_first_attribute(resource link, resource result_entry)
  973. Return first attribute */
  974. PHP_FUNCTION(ldap_first_attribute)
  975. {
  976. zval *link, *result_entry;
  977. ldap_linkdata *ld;
  978. ldap_resultentry *resultentry;
  979. char *attribute;
  980. long dummy_ber;
  981. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|l", &link, &result_entry, &dummy_ber) != SUCCESS) {
  982. return;
  983. }
  984. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  985. ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
  986. if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber)) == NULL) {
  987. RETURN_FALSE;
  988. } else {
  989. RETVAL_STRING(attribute, 1);
  990. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
  991. ldap_memfree(attribute);
  992. #endif
  993. }
  994. }
  995. /* }}} */
  996. /* {{{ proto string ldap_next_attribute(resource link, resource result_entry)
  997. Get the next attribute in result */
  998. PHP_FUNCTION(ldap_next_attribute)
  999. {
  1000. zval *link, *result_entry;
  1001. ldap_linkdata *ld;
  1002. ldap_resultentry *resultentry;
  1003. char *attribute;
  1004. long dummy_ber;
  1005. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|l", &link, &result_entry, &dummy_ber) != SUCCESS) {
  1006. return;
  1007. }
  1008. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1009. ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
  1010. if (resultentry->ber == NULL) {
  1011. php_error_docref(NULL TSRMLS_CC, E_WARNING, "called before calling ldap_first_attribute() or no attributes found in result entry");
  1012. RETURN_FALSE;
  1013. }
  1014. if ((attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber)) == NULL) {
  1015. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
  1016. if (resultentry->ber != NULL) {
  1017. ber_free(resultentry->ber, 0);
  1018. resultentry->ber = NULL;
  1019. }
  1020. #endif
  1021. RETURN_FALSE;
  1022. } else {
  1023. RETVAL_STRING(attribute, 1);
  1024. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
  1025. ldap_memfree(attribute);
  1026. #endif
  1027. }
  1028. }
  1029. /* }}} */
  1030. /* {{{ proto array ldap_get_attributes(resource link, resource result_entry)
  1031. Get attributes from a search result entry */
  1032. PHP_FUNCTION(ldap_get_attributes)
  1033. {
  1034. zval *link, *result_entry;
  1035. zval *tmp;
  1036. ldap_linkdata *ld;
  1037. ldap_resultentry *resultentry;
  1038. char *attribute;
  1039. struct berval **ldap_value;
  1040. int i, num_values, num_attrib;
  1041. BerElement *ber;
  1042. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result_entry) != SUCCESS) {
  1043. return;
  1044. }
  1045. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1046. ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
  1047. array_init(return_value);
  1048. num_attrib = 0;
  1049. attribute = ldap_first_attribute(ld->link, resultentry->data, &ber);
  1050. while (attribute != NULL) {
  1051. ldap_value = ldap_get_values_len(ld->link, resultentry->data, attribute);
  1052. num_values = ldap_count_values_len(ldap_value);
  1053. MAKE_STD_ZVAL(tmp);
  1054. array_init(tmp);
  1055. add_assoc_long(tmp, "count", num_values);
  1056. for (i = 0; i < num_values; i++) {
  1057. add_index_stringl(tmp, i, ldap_value[i]->bv_val, ldap_value[i]->bv_len, 1);
  1058. }
  1059. ldap_value_free_len(ldap_value);
  1060. zend_hash_update(Z_ARRVAL_P(return_value), attribute, strlen(attribute)+1, (void *) &tmp, sizeof(zval *), NULL);
  1061. add_index_string(return_value, num_attrib, attribute, 1);
  1062. num_attrib++;
  1063. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
  1064. ldap_memfree(attribute);
  1065. #endif
  1066. attribute = ldap_next_attribute(ld->link, resultentry->data, ber);
  1067. }
  1068. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
  1069. if (ber != NULL) {
  1070. ber_free(ber, 0);
  1071. }
  1072. #endif
  1073. add_assoc_long(return_value, "count", num_attrib);
  1074. }
  1075. /* }}} */
  1076. /* {{{ proto array ldap_get_values_len(resource link, resource result_entry, string attribute)
  1077. Get all values with lengths from a result entry */
  1078. PHP_FUNCTION(ldap_get_values_len)
  1079. {
  1080. zval *link, *result_entry;
  1081. ldap_linkdata *ld;
  1082. ldap_resultentry *resultentry;
  1083. char *attr;
  1084. struct berval **ldap_value_len;
  1085. int i, num_values, attr_len;
  1086. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrs", &link, &result_entry, &attr, &attr_len) != SUCCESS) {
  1087. return;
  1088. }
  1089. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1090. ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
  1091. if ((ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr)) == NULL) {
  1092. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot get the value(s) of attribute %s", ldap_err2string(_get_lderrno(ld->link)));
  1093. RETURN_FALSE;
  1094. }
  1095. num_values = ldap_count_values_len(ldap_value_len);
  1096. array_init(return_value);
  1097. for (i=0; i<num_values; i++) {
  1098. add_next_index_stringl(return_value, ldap_value_len[i]->bv_val, ldap_value_len[i]->bv_len, 1);
  1099. }
  1100. add_assoc_long(return_value, "count", num_values);
  1101. ldap_value_free_len(ldap_value_len);
  1102. }
  1103. /* }}} */
  1104. /* {{{ proto string ldap_get_dn(resource link, resource result_entry)
  1105. Get the DN of a result entry */
  1106. PHP_FUNCTION(ldap_get_dn)
  1107. {
  1108. zval *link, *result_entry;
  1109. ldap_linkdata *ld;
  1110. ldap_resultentry *resultentry;
  1111. char *text;
  1112. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result_entry) != SUCCESS) {
  1113. return;
  1114. }
  1115. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1116. ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
  1117. text = ldap_get_dn(ld->link, resultentry->data);
  1118. if (text != NULL) {
  1119. RETVAL_STRING(text, 1);
  1120. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
  1121. ldap_memfree(text);
  1122. #else
  1123. free(text);
  1124. #endif
  1125. } else {
  1126. RETURN_FALSE;
  1127. }
  1128. }
  1129. /* }}} */
  1130. /* {{{ proto array ldap_explode_dn(string dn, int with_attrib)
  1131. Splits DN into its component parts */
  1132. PHP_FUNCTION(ldap_explode_dn)
  1133. {
  1134. long with_attrib;
  1135. char *dn, **ldap_value;
  1136. int i, count, dn_len;
  1137. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &dn, &dn_len, &with_attrib) != SUCCESS) {
  1138. return;
  1139. }
  1140. if (!(ldap_value = ldap_explode_dn(dn, with_attrib))) {
  1141. /* Invalid parameters were passed to ldap_explode_dn */
  1142. RETURN_FALSE;
  1143. }
  1144. i=0;
  1145. while (ldap_value[i] != NULL) i++;
  1146. count = i;
  1147. array_init(return_value);
  1148. add_assoc_long(return_value, "count", count);
  1149. for (i = 0; i<count; i++) {
  1150. add_index_string(return_value, i, ldap_value[i], 1);
  1151. }
  1152. ldap_memvfree((void **)ldap_value);
  1153. }
  1154. /* }}} */
  1155. /* {{{ proto string ldap_dn2ufn(string dn)
  1156. Convert DN to User Friendly Naming format */
  1157. PHP_FUNCTION(ldap_dn2ufn)
  1158. {
  1159. char *dn, *ufn;
  1160. int dn_len;
  1161. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &dn, &dn_len) != SUCCESS) {
  1162. return;
  1163. }
  1164. ufn = ldap_dn2ufn(dn);
  1165. if (ufn != NULL) {
  1166. RETVAL_STRING(ufn, 1);
  1167. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP || WINDOWS
  1168. ldap_memfree(ufn);
  1169. #endif
  1170. } else {
  1171. RETURN_FALSE;
  1172. }
  1173. }
  1174. /* }}} */
  1175. /* added to fix use of ldap_modify_add for doing an ldap_add, gerrit thomson. */
  1176. #define PHP_LD_FULL_ADD 0xff
  1177. /* {{{ php_ldap_do_modify
  1178. */
  1179. static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper)
  1180. {
  1181. zval *link, *entry, **value, **ivalue;
  1182. ldap_linkdata *ld;
  1183. char *dn;
  1184. LDAPMod **ldap_mods;
  1185. int i, j, num_attribs, num_values, dn_len;
  1186. int *num_berval;
  1187. char *attribute;
  1188. ulong index;
  1189. int is_full_add=0; /* flag for full add operation so ldap_mod_add can be put back into oper, gerrit THomson */
  1190. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsa", &link, &dn, &dn_len, &entry) != SUCCESS) {
  1191. return;
  1192. }
  1193. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1194. num_attribs = zend_hash_num_elements(Z_ARRVAL_P(entry));
  1195. ldap_mods = safe_emalloc((num_attribs+1), sizeof(LDAPMod *), 0);
  1196. num_berval = safe_emalloc(num_attribs, sizeof(int), 0);
  1197. zend_hash_internal_pointer_reset(Z_ARRVAL_P(entry));
  1198. /* added by gerrit thomson to fix ldap_add using ldap_mod_add */
  1199. if (oper == PHP_LD_FULL_ADD) {
  1200. oper = LDAP_MOD_ADD;
  1201. is_full_add = 1;
  1202. }
  1203. /* end additional , gerrit thomson */
  1204. for (i = 0; i < num_attribs; i++) {
  1205. ldap_mods[i] = emalloc(sizeof(LDAPMod));
  1206. ldap_mods[i]->mod_op = oper | LDAP_MOD_BVALUES;
  1207. ldap_mods[i]->mod_type = NULL;
  1208. if (zend_hash_get_current_key(Z_ARRVAL_P(entry), &attribute, &index, 0) == HASH_KEY_IS_STRING) {
  1209. ldap_mods[i]->mod_type = estrdup(attribute);
  1210. } else {
  1211. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown attribute in the data");
  1212. /* Free allocated memory */
  1213. while (i >= 0) {
  1214. if (ldap_mods[i]->mod_type) {
  1215. efree(ldap_mods[i]->mod_type);
  1216. }
  1217. efree(ldap_mods[i]);
  1218. i--;
  1219. }
  1220. efree(num_berval);
  1221. efree(ldap_mods);
  1222. RETURN_FALSE;
  1223. }
  1224. zend_hash_get_current_data(Z_ARRVAL_P(entry), (void **)&value);
  1225. if (Z_TYPE_PP(value) != IS_ARRAY) {
  1226. num_values = 1;
  1227. } else {
  1228. num_values = zend_hash_num_elements(Z_ARRVAL_PP(value));
  1229. }
  1230. num_berval[i] = num_values;
  1231. ldap_mods[i]->mod_bvalues = safe_emalloc((num_values + 1), sizeof(struct berval *), 0);
  1232. /* allow for arrays with one element, no allowance for arrays with none but probably not required, gerrit thomson. */
  1233. if ((num_values == 1) && (Z_TYPE_PP(value) != IS_ARRAY)) {
  1234. convert_to_string_ex(value);
  1235. ldap_mods[i]->mod_bvalues[0] = (struct berval *) emalloc (sizeof(struct berval));
  1236. ldap_mods[i]->mod_bvalues[0]->bv_len = Z_STRLEN_PP(value);
  1237. ldap_mods[i]->mod_bvalues[0]->bv_val = Z_STRVAL_PP(value);
  1238. } else {
  1239. for (j = 0; j < num_values; j++) {
  1240. if (zend_hash_index_find(Z_ARRVAL_PP(value), j, (void **) &ivalue) != SUCCESS) {
  1241. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Value array must have consecutive indices 0, 1, ...");
  1242. num_berval[i] = j;
  1243. num_attribs = i + 1;
  1244. RETVAL_FALSE;
  1245. goto errexit;
  1246. }
  1247. convert_to_string_ex(ivalue);
  1248. ldap_mods[i]->mod_bvalues[j] = (struct berval *) emalloc (sizeof(struct berval));
  1249. ldap_mods[i]->mod_bvalues[j]->bv_len = Z_STRLEN_PP(ivalue);
  1250. ldap_mods[i]->mod_bvalues[j]->bv_val = Z_STRVAL_PP(ivalue);
  1251. }
  1252. }
  1253. ldap_mods[i]->mod_bvalues[num_values] = NULL;
  1254. zend_hash_move_forward(Z_ARRVAL_P(entry));
  1255. }
  1256. ldap_mods[num_attribs] = NULL;
  1257. /* check flag to see if do_mod was called to perform full add , gerrit thomson */
  1258. if (is_full_add == 1) {
  1259. if ((i = ldap_add_ext_s(ld->link, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS) {
  1260. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Add: %s", ldap_err2string(i));
  1261. RETVAL_FALSE;
  1262. } else RETVAL_TRUE;
  1263. } else {
  1264. if ((i = ldap_modify_ext_s(ld->link, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS) {
  1265. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Modify: %s", ldap_err2string(i));
  1266. RETVAL_FALSE;
  1267. } else RETVAL_TRUE;
  1268. }
  1269. errexit:
  1270. for (i = 0; i < num_attribs; i++) {
  1271. efree(ldap_mods[i]->mod_type);
  1272. for (j = 0; j < num_berval[i]; j++) {
  1273. efree(ldap_mods[i]->mod_bvalues[j]);
  1274. }
  1275. efree(ldap_mods[i]->mod_bvalues);
  1276. efree(ldap_mods[i]);
  1277. }
  1278. efree(num_berval);
  1279. efree(ldap_mods);
  1280. return;
  1281. }
  1282. /* }}} */
  1283. /* {{{ proto bool ldap_add(resource link, string dn, array entry)
  1284. Add entries to LDAP directory */
  1285. PHP_FUNCTION(ldap_add)
  1286. {
  1287. /* use a newly define parameter into the do_modify so ldap_mod_add can be used the way it is supposed to be used , Gerrit THomson */
  1288. php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_LD_FULL_ADD);
  1289. }
  1290. /* }}} */
  1291. /* three functions for attribute base modifications, gerrit Thomson */
  1292. /* {{{ proto bool ldap_mod_replace(resource link, string dn, array entry)
  1293. Replace attribute values with new ones */
  1294. PHP_FUNCTION(ldap_mod_replace)
  1295. {
  1296. php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_MOD_REPLACE);
  1297. }
  1298. /* }}} */
  1299. /* {{{ proto bool ldap_mod_add(resource link, string dn, array entry)
  1300. Add attribute values to current */
  1301. PHP_FUNCTION(ldap_mod_add)
  1302. {
  1303. php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_MOD_ADD);
  1304. }
  1305. /* }}} */
  1306. /* {{{ proto bool ldap_mod_del(resource link, string dn, array entry)
  1307. Delete attribute values */
  1308. PHP_FUNCTION(ldap_mod_del)
  1309. {
  1310. php_ldap_do_modify(INTERNAL_FUNCTION_PARAM_PASSTHRU, LDAP_MOD_DELETE);
  1311. }
  1312. /* }}} */
  1313. /* {{{ proto bool ldap_delete(resource link, string dn)
  1314. Delete an entry from a directory */
  1315. PHP_FUNCTION(ldap_delete)
  1316. {
  1317. zval *link;
  1318. ldap_linkdata *ld;
  1319. char *dn;
  1320. int rc, dn_len;
  1321. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &link, &dn, &dn_len) != SUCCESS) {
  1322. return;
  1323. }
  1324. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1325. if ((rc = ldap_delete_ext_s(ld->link, dn, NULL, NULL)) != LDAP_SUCCESS) {
  1326. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Delete: %s", ldap_err2string(rc));
  1327. RETURN_FALSE;
  1328. }
  1329. RETURN_TRUE;
  1330. }
  1331. /* }}} */
  1332. /* {{{ _ldap_str_equal_to_const
  1333. */
  1334. static int _ldap_str_equal_to_const(const char *str, uint str_len, const char *cstr)
  1335. {
  1336. int i;
  1337. if (strlen(cstr) != str_len)
  1338. return 0;
  1339. for (i = 0; i < str_len; ++i) {
  1340. if (str[i] != cstr[i]) {
  1341. return 0;
  1342. }
  1343. }
  1344. return 1;
  1345. }
  1346. /* }}} */
  1347. /* {{{ _ldap_strlen_max
  1348. */
  1349. static int _ldap_strlen_max(const char *str, uint max_len)
  1350. {
  1351. int i;
  1352. for (i = 0; i < max_len; ++i) {
  1353. if (str[i] == '\0') {
  1354. return i;
  1355. }
  1356. }
  1357. return max_len;
  1358. }
  1359. /* }}} */
  1360. /* {{{ _ldap_hash_fetch
  1361. */
  1362. static void _ldap_hash_fetch(zval *hashTbl, const char *key, zval **out)
  1363. {
  1364. zval **fetched;
  1365. if (zend_hash_find(Z_ARRVAL_P(hashTbl), key, strlen(key)+1, (void **) &fetched) == SUCCESS) {
  1366. *out = *fetched;
  1367. }
  1368. else {
  1369. *out = NULL;
  1370. }
  1371. }
  1372. /* }}} */
  1373. /* {{{ proto bool ldap_modify_batch(resource link, string dn, array modifs)
  1374. Perform multiple modifications as part of one operation */
  1375. PHP_FUNCTION(ldap_modify_batch)
  1376. {
  1377. ldap_linkdata *ld;
  1378. zval *link, *mods, *mod, *modinfo, *modval;
  1379. zval *attrib, *modtype, *vals;
  1380. zval **fetched;
  1381. char *dn;
  1382. int dn_len;
  1383. int i, j, k;
  1384. int num_mods, num_modprops, num_modvals;
  1385. LDAPMod **ldap_mods;
  1386. uint oper;
  1387. /*
  1388. $mods = array(
  1389. array(
  1390. "attrib" => "unicodePwd",
  1391. "modtype" => LDAP_MODIFY_BATCH_REMOVE,
  1392. "values" => array($oldpw)
  1393. ),
  1394. array(
  1395. "attrib" => "unicodePwd",
  1396. "modtype" => LDAP_MODIFY_BATCH_ADD,
  1397. "values" => array($newpw)
  1398. ),
  1399. array(
  1400. "attrib" => "userPrincipalName",
  1401. "modtype" => LDAP_MODIFY_BATCH_REPLACE,
  1402. "values" => array("janitor@corp.contoso.com")
  1403. ),
  1404. array(
  1405. "attrib" => "userCert",
  1406. "modtype" => LDAP_MODIFY_BATCH_REMOVE_ALL
  1407. )
  1408. );
  1409. */
  1410. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsa", &link, &dn, &dn_len, &mods) != SUCCESS) {
  1411. return;
  1412. }
  1413. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1414. /* perform validation */
  1415. {
  1416. char *modkey;
  1417. uint modkeylen;
  1418. long modtype;
  1419. /* to store the wrongly-typed keys */
  1420. ulong tmpUlong;
  1421. /* make sure the DN contains no NUL bytes */
  1422. if (_ldap_strlen_max(dn, dn_len) != dn_len) {
  1423. php_error_docref(NULL TSRMLS_CC, E_WARNING, "DN must not contain NUL bytes");
  1424. RETURN_FALSE;
  1425. }
  1426. /* make sure the top level is a normal array */
  1427. zend_hash_internal_pointer_reset(Z_ARRVAL_P(mods));
  1428. if (zend_hash_get_current_key_type(Z_ARRVAL_P(mods)) != HASH_KEY_IS_LONG) {
  1429. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Modifications array must not be string-indexed");
  1430. RETURN_FALSE;
  1431. }
  1432. num_mods = zend_hash_num_elements(Z_ARRVAL_P(mods));
  1433. for (i = 0; i < num_mods; i++) {
  1434. /* is the numbering consecutive? */
  1435. if (zend_hash_index_find(Z_ARRVAL_P(mods), i, (void **) &fetched) != SUCCESS) {
  1436. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Modifications array must have consecutive indices 0, 1, ...");
  1437. RETURN_FALSE;
  1438. }
  1439. mod = *fetched;
  1440. /* is it an array? */
  1441. if (Z_TYPE_P(mod) != IS_ARRAY) {
  1442. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Each entry of modifications array must be an array itself");
  1443. RETURN_FALSE;
  1444. }
  1445. /* for the modification hashtable... */
  1446. zend_hash_internal_pointer_reset(Z_ARRVAL_P(mod));
  1447. num_modprops = zend_hash_num_elements(Z_ARRVAL_P(mod));
  1448. for (j = 0; j < num_modprops; j++) {
  1449. /* are the keys strings? */
  1450. if (zend_hash_get_current_key_ex(Z_ARRVAL_P(mod), &modkey, &modkeylen, &tmpUlong, 0, NULL) != HASH_KEY_IS_STRING) {
  1451. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Each entry of modifications array must be string-indexed");
  1452. RETURN_FALSE;
  1453. }
  1454. /* modkeylen includes the terminating NUL byte; remove that */
  1455. --modkeylen;
  1456. /* is this a valid entry? */
  1457. if (
  1458. !_ldap_str_equal_to_const(modkey, modkeylen, LDAP_MODIFY_BATCH_ATTRIB) &&
  1459. !_ldap_str_equal_to_const(modkey, modkeylen, LDAP_MODIFY_BATCH_MODTYPE) &&
  1460. !_ldap_str_equal_to_const(modkey, modkeylen, LDAP_MODIFY_BATCH_VALUES)
  1461. ) {
  1462. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The only allowed keys in entries of the modifications array are '" LDAP_MODIFY_BATCH_ATTRIB "', '" LDAP_MODIFY_BATCH_MODTYPE "' and '" LDAP_MODIFY_BATCH_VALUES "'");
  1463. RETURN_FALSE;
  1464. }
  1465. zend_hash_get_current_data(Z_ARRVAL_P(mod), (void **) &fetched);
  1466. modinfo = *fetched;
  1467. /* does the value type match the key? */
  1468. if (_ldap_str_equal_to_const(modkey, modkeylen, LDAP_MODIFY_BATCH_ATTRIB)) {
  1469. if (Z_TYPE_P(modinfo) != IS_STRING) {
  1470. php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_ATTRIB "' value must be a string");
  1471. RETURN_FALSE;
  1472. }
  1473. if (Z_STRLEN_P(modinfo) != _ldap_strlen_max(Z_STRVAL_P(modinfo), Z_STRLEN_P(modinfo))) {
  1474. php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_ATTRIB "' value must not contain NUL bytes");
  1475. RETURN_FALSE;
  1476. }
  1477. }
  1478. else if (_ldap_str_equal_to_const(modkey, modkeylen, LDAP_MODIFY_BATCH_MODTYPE)) {
  1479. if (Z_TYPE_P(modinfo) != IS_LONG) {
  1480. php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_MODTYPE "' value must be a long");
  1481. RETURN_FALSE;
  1482. }
  1483. /* is the value in range? */
  1484. modtype = Z_LVAL_P(modinfo);
  1485. if (
  1486. modtype != LDAP_MODIFY_BATCH_ADD &&
  1487. modtype != LDAP_MODIFY_BATCH_REMOVE &&
  1488. modtype != LDAP_MODIFY_BATCH_REPLACE &&
  1489. modtype != LDAP_MODIFY_BATCH_REMOVE_ALL
  1490. ) {
  1491. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The '" LDAP_MODIFY_BATCH_MODTYPE "' value must match one of the LDAP_MODIFY_BATCH_* constants");
  1492. RETURN_FALSE;
  1493. }
  1494. /* if it's REMOVE_ALL, there must not be a values array; otherwise, there must */
  1495. if (modtype == LDAP_MODIFY_BATCH_REMOVE_ALL) {
  1496. if (zend_hash_exists(Z_ARRVAL_P(mod), LDAP_MODIFY_BATCH_VALUES, strlen(LDAP_MODIFY_BATCH_VALUES) + 1)) {
  1497. php_error_docref(NULL TSRMLS_CC, E_WARNING, "If '" LDAP_MODIFY_BATCH_MODTYPE "' is LDAP_MODIFY_BATCH_REMOVE_ALL, a '" LDAP_MODIFY_BATCH_VALUES "' array must not be provided");
  1498. RETURN_FALSE;
  1499. }
  1500. }
  1501. else {
  1502. if (!zend_hash_exists(Z_ARRVAL_P(mod), LDAP_MODIFY_BATCH_VALUES, strlen(LDAP_MODIFY_BATCH_VALUES) + 1)) {
  1503. php_error_docref(NULL TSRMLS_CC, E_WARNING, "If '" LDAP_MODIFY_BATCH_MODTYPE "' is not LDAP_MODIFY_BATCH_REMOVE_ALL, a '" LDAP_MODIFY_BATCH_VALUES "' array must be provided");
  1504. RETURN_FALSE;
  1505. }
  1506. }
  1507. }
  1508. else if (_ldap_str_equal_to_const(modkey, modkeylen, LDAP_MODIFY_BATCH_VALUES)) {
  1509. if (Z_TYPE_P(modinfo) != IS_ARRAY) {
  1510. php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_VALUES "' value must be an array");
  1511. RETURN_FALSE;
  1512. }
  1513. /* is the array not empty? */
  1514. zend_hash_internal_pointer_reset(Z_ARRVAL_P(modinfo));
  1515. num_modvals = zend_hash_num_elements(Z_ARRVAL_P(modinfo));
  1516. if (num_modvals == 0) {
  1517. php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_VALUES "' array must have at least one element");
  1518. RETURN_FALSE;
  1519. }
  1520. /* are its keys integers? */
  1521. if (zend_hash_get_current_key_type(Z_ARRVAL_P(modinfo)) != HASH_KEY_IS_LONG) {
  1522. php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_VALUES "' array must not be string-indexed");
  1523. RETURN_FALSE;
  1524. }
  1525. /* are the keys consecutive? */
  1526. for (k = 0; k < num_modvals; k++) {
  1527. if (zend_hash_index_find(Z_ARRVAL_P(modinfo), k, (void **) &fetched) != SUCCESS) {
  1528. php_error_docref(NULL TSRMLS_CC, E_WARNING, "A '" LDAP_MODIFY_BATCH_VALUES "' array must have consecutive indices 0, 1, ...");
  1529. RETURN_FALSE;
  1530. }
  1531. modval = *fetched;
  1532. /* is the data element a string? */
  1533. if (Z_TYPE_P(modval) != IS_STRING) {
  1534. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Each element of a '" LDAP_MODIFY_BATCH_VALUES "' array must be a string");
  1535. RETURN_FALSE;
  1536. }
  1537. }
  1538. }
  1539. zend_hash_move_forward(Z_ARRVAL_P(mod));
  1540. }
  1541. }
  1542. }
  1543. /* validation was successful */
  1544. /* allocate array of modifications */
  1545. ldap_mods = safe_emalloc((num_mods+1), sizeof(LDAPMod *), 0);
  1546. /* for each modification */
  1547. for (i = 0; i < num_mods; i++) {
  1548. /* allocate the modification struct */
  1549. ldap_mods[i] = safe_emalloc(1, sizeof(LDAPMod), 0);
  1550. /* fetch the relevant data */
  1551. zend_hash_index_find(Z_ARRVAL_P(mods), i, (void **) &fetched);
  1552. mod = *fetched;
  1553. _ldap_hash_fetch(mod, LDAP_MODIFY_BATCH_ATTRIB, &attrib);
  1554. _ldap_hash_fetch(mod, LDAP_MODIFY_BATCH_MODTYPE, &modtype);
  1555. _ldap_hash_fetch(mod, LDAP_MODIFY_BATCH_VALUES, &vals);
  1556. /* map the modification type */
  1557. switch (Z_LVAL_P(modtype)) {
  1558. case LDAP_MODIFY_BATCH_ADD:
  1559. oper = LDAP_MOD_ADD;
  1560. break;
  1561. case LDAP_MODIFY_BATCH_REMOVE:
  1562. case LDAP_MODIFY_BATCH_REMOVE_ALL:
  1563. oper = LDAP_MOD_DELETE;
  1564. break;
  1565. case LDAP_MODIFY_BATCH_REPLACE:
  1566. oper = LDAP_MOD_REPLACE;
  1567. break;
  1568. default:
  1569. php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unknown and uncaught modification type.");
  1570. RETURN_FALSE;
  1571. }
  1572. /* fill in the basic info */
  1573. ldap_mods[i]->mod_op = oper | LDAP_MOD_BVALUES;
  1574. ldap_mods[i]->mod_type = estrndup(Z_STRVAL_P(attrib), Z_STRLEN_P(attrib));
  1575. if (Z_LVAL_P(modtype) == LDAP_MODIFY_BATCH_REMOVE_ALL) {
  1576. /* no values */
  1577. ldap_mods[i]->mod_bvalues = NULL;
  1578. }
  1579. else {
  1580. /* allocate space for the values as part of this modification */
  1581. num_modvals = zend_hash_num_elements(Z_ARRVAL_P(vals));
  1582. ldap_mods[i]->mod_bvalues = safe_emalloc((num_modvals+1), sizeof(struct berval *), 0);
  1583. /* for each value */
  1584. for (j = 0; j < num_modvals; j++) {
  1585. /* fetch it */
  1586. zend_hash_index_find(Z_ARRVAL_P(vals), j, (void **) &fetched);
  1587. modval = *fetched;
  1588. /* allocate the data struct */
  1589. ldap_mods[i]->mod_bvalues[j] = safe_emalloc(1, sizeof(struct berval), 0);
  1590. /* fill it */
  1591. ldap_mods[i]->mod_bvalues[j]->bv_len = Z_STRLEN_P(modval);
  1592. ldap_mods[i]->mod_bvalues[j]->bv_val = estrndup(Z_STRVAL_P(modval), Z_STRLEN_P(modval));
  1593. }
  1594. /* NULL-terminate values */
  1595. ldap_mods[i]->mod_bvalues[num_modvals] = NULL;
  1596. }
  1597. }
  1598. /* NULL-terminate modifications */
  1599. ldap_mods[num_mods] = NULL;
  1600. /* perform (finally) */
  1601. if ((i = ldap_modify_ext_s(ld->link, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS) {
  1602. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Batch Modify: %s", ldap_err2string(i));
  1603. RETVAL_FALSE;
  1604. } else RETVAL_TRUE;
  1605. /* clean up */
  1606. {
  1607. for (i = 0; i < num_mods; i++) {
  1608. /* attribute */
  1609. efree(ldap_mods[i]->mod_type);
  1610. if (ldap_mods[i]->mod_bvalues != NULL) {
  1611. /* each BER value */
  1612. for (j = 0; ldap_mods[i]->mod_bvalues[j] != NULL; j++) {
  1613. /* free the data bytes */
  1614. efree(ldap_mods[i]->mod_bvalues[j]->bv_val);
  1615. /* free the bvalue struct */
  1616. efree(ldap_mods[i]->mod_bvalues[j]);
  1617. }
  1618. /* the BER value array */
  1619. efree(ldap_mods[i]->mod_bvalues);
  1620. }
  1621. /* the modification */
  1622. efree(ldap_mods[i]);
  1623. }
  1624. /* the modifications array */
  1625. efree(ldap_mods);
  1626. }
  1627. }
  1628. /* }}} */
  1629. /* {{{ proto int ldap_errno(resource link)
  1630. Get the current ldap error number */
  1631. PHP_FUNCTION(ldap_errno)
  1632. {
  1633. zval *link;
  1634. ldap_linkdata *ld;
  1635. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &link) != SUCCESS) {
  1636. return;
  1637. }
  1638. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1639. RETURN_LONG(_get_lderrno(ld->link));
  1640. }
  1641. /* }}} */
  1642. /* {{{ proto string ldap_err2str(int errno)
  1643. Convert error number to error string */
  1644. PHP_FUNCTION(ldap_err2str)
  1645. {
  1646. long perrno;
  1647. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &perrno) != SUCCESS) {
  1648. return;
  1649. }
  1650. RETURN_STRING(ldap_err2string(perrno), 1);
  1651. }
  1652. /* }}} */
  1653. /* {{{ proto string ldap_error(resource link)
  1654. Get the current ldap error string */
  1655. PHP_FUNCTION(ldap_error)
  1656. {
  1657. zval *link;
  1658. ldap_linkdata *ld;
  1659. int ld_errno;
  1660. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &link) != SUCCESS) {
  1661. return;
  1662. }
  1663. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1664. ld_errno = _get_lderrno(ld->link);
  1665. RETURN_STRING(ldap_err2string(ld_errno), 1);
  1666. }
  1667. /* }}} */
  1668. /* {{{ proto bool ldap_compare(resource link, string dn, string attr, string value)
  1669. Determine if an entry has a specific value for one of its attributes */
  1670. PHP_FUNCTION(ldap_compare)
  1671. {
  1672. zval *link;
  1673. char *dn, *attr, *value;
  1674. int dn_len, attr_len, value_len;
  1675. ldap_linkdata *ld;
  1676. int errno;
  1677. struct berval lvalue;
  1678. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsss", &link, &dn, &dn_len, &attr, &attr_len, &value, &value_len) != SUCCESS) {
  1679. return;
  1680. }
  1681. lvalue.bv_val = value;
  1682. lvalue.bv_len = value_len;
  1683. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1684. errno = ldap_compare_ext_s(ld->link, dn, attr, &lvalue, NULL, NULL);
  1685. switch (errno) {
  1686. case LDAP_COMPARE_TRUE:
  1687. RETURN_TRUE;
  1688. break;
  1689. case LDAP_COMPARE_FALSE:
  1690. RETURN_FALSE;
  1691. break;
  1692. }
  1693. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Compare: %s", ldap_err2string(errno));
  1694. RETURN_LONG(-1);
  1695. }
  1696. /* }}} */
  1697. /* {{{ proto bool ldap_sort(resource link, resource result, string sortfilter)
  1698. Sort LDAP result entries */
  1699. PHP_FUNCTION(ldap_sort)
  1700. {
  1701. zval *link, *result;
  1702. ldap_linkdata *ld;
  1703. char *sortfilter;
  1704. int sflen;
  1705. zend_rsrc_list_entry *le;
  1706. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrs", &link, &result, &sortfilter, &sflen) != SUCCESS) {
  1707. RETURN_FALSE;
  1708. }
  1709. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1710. if (zend_hash_index_find(&EG(regular_list), Z_LVAL_P(result), (void **) &le) != SUCCESS || le->type != le_result) {
  1711. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied resource is not a valid ldap result resource");
  1712. RETURN_FALSE;
  1713. }
  1714. if (ldap_sort_entries(ld->link, (LDAPMessage **) &le->ptr, sflen ? sortfilter : NULL, strcmp) != LDAP_SUCCESS) {
  1715. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ldap_err2string(errno));
  1716. RETURN_FALSE;
  1717. }
  1718. RETURN_TRUE;
  1719. }
  1720. /* }}} */
  1721. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP
  1722. /* {{{ proto bool ldap_get_option(resource link, int option, mixed retval)
  1723. Get the current value of various session-wide parameters */
  1724. PHP_FUNCTION(ldap_get_option)
  1725. {
  1726. zval *link, *retval;
  1727. ldap_linkdata *ld;
  1728. long option;
  1729. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz", &link, &option, &retval) != SUCCESS) {
  1730. return;
  1731. }
  1732. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1733. switch (option) {
  1734. /* options with int value */
  1735. case LDAP_OPT_DEREF:
  1736. case LDAP_OPT_SIZELIMIT:
  1737. case LDAP_OPT_TIMELIMIT:
  1738. case LDAP_OPT_PROTOCOL_VERSION:
  1739. case LDAP_OPT_ERROR_NUMBER:
  1740. case LDAP_OPT_REFERRALS:
  1741. #ifdef LDAP_OPT_RESTART
  1742. case LDAP_OPT_RESTART:
  1743. #endif
  1744. {
  1745. int val;
  1746. if (ldap_get_option(ld->link, option, &val)) {
  1747. RETURN_FALSE;
  1748. }
  1749. zval_dtor(retval);
  1750. ZVAL_LONG(retval, val);
  1751. } break;
  1752. #ifdef LDAP_OPT_NETWORK_TIMEOUT
  1753. case LDAP_OPT_NETWORK_TIMEOUT:
  1754. {
  1755. struct timeval *timeout = NULL;
  1756. if (ldap_get_option(ld->link, LDAP_OPT_NETWORK_TIMEOUT, (void *) &timeout)) {
  1757. if (timeout) {
  1758. ldap_memfree(timeout);
  1759. }
  1760. RETURN_FALSE;
  1761. }
  1762. if (!timeout) {
  1763. RETURN_FALSE;
  1764. }
  1765. zval_dtor(retval);
  1766. ZVAL_LONG(retval, timeout->tv_sec);
  1767. ldap_memfree(timeout);
  1768. } break;
  1769. #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
  1770. case LDAP_X_OPT_CONNECT_TIMEOUT:
  1771. {
  1772. int timeout;
  1773. if (ldap_get_option(ld->link, LDAP_X_OPT_CONNECT_TIMEOUT, &timeout)) {
  1774. RETURN_FALSE;
  1775. }
  1776. zval_dtor(retval);
  1777. ZVAL_LONG(retval, (timeout / 1000));
  1778. } break;
  1779. #endif
  1780. #ifdef LDAP_OPT_TIMEOUT
  1781. case LDAP_OPT_TIMEOUT:
  1782. {
  1783. struct timeval *timeout = NULL;
  1784. if (ldap_get_option(ld->link, LDAP_OPT_TIMEOUT, (void *) &timeout)) {
  1785. if (timeout) {
  1786. ldap_memfree(timeout);
  1787. }
  1788. RETURN_FALSE;
  1789. }
  1790. if (!timeout) {
  1791. RETURN_FALSE;
  1792. }
  1793. zval_dtor(retval);
  1794. ZVAL_LONG(retval, timeout->tv_sec);
  1795. ldap_memfree(timeout);
  1796. } break;
  1797. #endif
  1798. /* options with string value */
  1799. case LDAP_OPT_ERROR_STRING:
  1800. #ifdef LDAP_OPT_HOST_NAME
  1801. case LDAP_OPT_HOST_NAME:
  1802. #endif
  1803. #ifdef HAVE_LDAP_SASL
  1804. case LDAP_OPT_X_SASL_MECH:
  1805. case LDAP_OPT_X_SASL_REALM:
  1806. case LDAP_OPT_X_SASL_AUTHCID:
  1807. case LDAP_OPT_X_SASL_AUTHZID:
  1808. #endif
  1809. #ifdef LDAP_OPT_MATCHED_DN
  1810. case LDAP_OPT_MATCHED_DN:
  1811. #endif
  1812. {
  1813. char *val = NULL;
  1814. if (ldap_get_option(ld->link, option, &val) || val == NULL || *val == '\0') {
  1815. if (val) {
  1816. ldap_memfree(val);
  1817. }
  1818. RETURN_FALSE;
  1819. }
  1820. zval_dtor(retval);
  1821. ZVAL_STRING(retval, val, 1);
  1822. ldap_memfree(val);
  1823. } break;
  1824. /* options not implemented
  1825. case LDAP_OPT_SERVER_CONTROLS:
  1826. case LDAP_OPT_CLIENT_CONTROLS:
  1827. case LDAP_OPT_API_INFO:
  1828. case LDAP_OPT_API_FEATURE_INFO:
  1829. */
  1830. default:
  1831. RETURN_FALSE;
  1832. }
  1833. RETURN_TRUE;
  1834. }
  1835. /* }}} */
  1836. /* {{{ proto bool ldap_set_option(resource link, int option, mixed newval)
  1837. Set the value of various session-wide parameters */
  1838. PHP_FUNCTION(ldap_set_option)
  1839. {
  1840. zval *link, **newval;
  1841. ldap_linkdata *ld;
  1842. LDAP *ldap;
  1843. long option;
  1844. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zlZ", &link, &option, &newval) != SUCCESS) {
  1845. return;
  1846. }
  1847. if (Z_TYPE_P(link) == IS_NULL) {
  1848. ldap = NULL;
  1849. } else {
  1850. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  1851. ldap = ld->link;
  1852. }
  1853. switch (option) {
  1854. /* options with int value */
  1855. case LDAP_OPT_DEREF:
  1856. case LDAP_OPT_SIZELIMIT:
  1857. case LDAP_OPT_TIMELIMIT:
  1858. case LDAP_OPT_PROTOCOL_VERSION:
  1859. case LDAP_OPT_ERROR_NUMBER:
  1860. #ifdef LDAP_OPT_DEBUG_LEVEL
  1861. case LDAP_OPT_DEBUG_LEVEL:
  1862. #endif
  1863. {
  1864. int val;
  1865. convert_to_long_ex(newval);
  1866. val = Z_LVAL_PP(newval);
  1867. if (ldap_set_option(ldap, option, &val)) {
  1868. RETURN_FALSE;
  1869. }
  1870. } break;
  1871. #ifdef LDAP_OPT_NETWORK_TIMEOUT
  1872. case LDAP_OPT_NETWORK_TIMEOUT:
  1873. {
  1874. struct timeval timeout;
  1875. convert_to_long_ex(newval);
  1876. timeout.tv_sec = Z_LVAL_PP(newval);
  1877. timeout.tv_usec = 0;
  1878. if (ldap_set_option(ldap, LDAP_OPT_NETWORK_TIMEOUT, (void *) &timeout)) {
  1879. RETURN_FALSE;
  1880. }
  1881. } break;
  1882. #elif defined(LDAP_X_OPT_CONNECT_TIMEOUT)
  1883. case LDAP_X_OPT_CONNECT_TIMEOUT:
  1884. {
  1885. int timeout;
  1886. convert_to_long_ex(newval);
  1887. timeout = 1000 * Z_LVAL_PP(newval); /* Convert to milliseconds */
  1888. if (ldap_set_option(ldap, LDAP_X_OPT_CONNECT_TIMEOUT, &timeout)) {
  1889. RETURN_FALSE;
  1890. }
  1891. } break;
  1892. #endif
  1893. #ifdef LDAP_OPT_TIMEOUT
  1894. case LDAP_OPT_TIMEOUT:
  1895. {
  1896. struct timeval timeout;
  1897. convert_to_long_ex(newval);
  1898. timeout.tv_sec = Z_LVAL_PP(newval);
  1899. timeout.tv_usec = 0;
  1900. if (ldap_set_option(ldap, LDAP_OPT_TIMEOUT, (void *) &timeout)) {
  1901. RETURN_FALSE;
  1902. }
  1903. } break;
  1904. #endif
  1905. /* options with string value */
  1906. case LDAP_OPT_ERROR_STRING:
  1907. #ifdef LDAP_OPT_HOST_NAME
  1908. case LDAP_OPT_HOST_NAME:
  1909. #endif
  1910. #ifdef HAVE_LDAP_SASL
  1911. case LDAP_OPT_X_SASL_MECH:
  1912. case LDAP_OPT_X_SASL_REALM:
  1913. case LDAP_OPT_X_SASL_AUTHCID:
  1914. case LDAP_OPT_X_SASL_AUTHZID:
  1915. #endif
  1916. #ifdef LDAP_OPT_MATCHED_DN
  1917. case LDAP_OPT_MATCHED_DN:
  1918. #endif
  1919. {
  1920. char *val;
  1921. convert_to_string_ex(newval);
  1922. val = Z_STRVAL_PP(newval);
  1923. if (ldap_set_option(ldap, option, val)) {
  1924. RETURN_FALSE;
  1925. }
  1926. } break;
  1927. /* options with boolean value */
  1928. case LDAP_OPT_REFERRALS:
  1929. #ifdef LDAP_OPT_RESTART
  1930. case LDAP_OPT_RESTART:
  1931. #endif
  1932. {
  1933. void *val;
  1934. convert_to_boolean_ex(newval);
  1935. val = Z_LVAL_PP(newval)
  1936. ? LDAP_OPT_ON : LDAP_OPT_OFF;
  1937. if (ldap_set_option(ldap, option, val)) {
  1938. RETURN_FALSE;
  1939. }
  1940. } break;
  1941. /* options with control list value */
  1942. case LDAP_OPT_SERVER_CONTROLS:
  1943. case LDAP_OPT_CLIENT_CONTROLS:
  1944. {
  1945. LDAPControl *ctrl, **ctrls, **ctrlp;
  1946. zval **ctrlval, **val;
  1947. int ncontrols;
  1948. char error=0;
  1949. if ((Z_TYPE_PP(newval) != IS_ARRAY) || !(ncontrols = zend_hash_num_elements(Z_ARRVAL_PP(newval)))) {
  1950. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected non-empty array value for this option");
  1951. RETURN_FALSE;
  1952. }
  1953. ctrls = safe_emalloc((1 + ncontrols), sizeof(*ctrls), 0);
  1954. *ctrls = NULL;
  1955. ctrlp = ctrls;
  1956. zend_hash_internal_pointer_reset(Z_ARRVAL_PP(newval));
  1957. while (zend_hash_get_current_data(Z_ARRVAL_PP(newval), (void**)&ctrlval) == SUCCESS) {
  1958. if (Z_TYPE_PP(ctrlval) != IS_ARRAY) {
  1959. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The array value must contain only arrays, where each array is a control");
  1960. error = 1;
  1961. break;
  1962. }
  1963. if (zend_hash_find(Z_ARRVAL_PP(ctrlval), "oid", sizeof("oid"), (void **) &val) != SUCCESS) {
  1964. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Control must have an oid key");
  1965. error = 1;
  1966. break;
  1967. }
  1968. ctrl = *ctrlp = emalloc(sizeof(**ctrlp));
  1969. convert_to_string_ex(val);
  1970. ctrl->ldctl_oid = Z_STRVAL_PP(val);
  1971. if (zend_hash_find(Z_ARRVAL_PP(ctrlval), "value", sizeof("value"), (void **) &val) == SUCCESS) {
  1972. convert_to_string_ex(val);
  1973. ctrl->ldctl_value.bv_val = Z_STRVAL_PP(val);
  1974. ctrl->ldctl_value.bv_len = Z_STRLEN_PP(val);
  1975. } else {
  1976. ctrl->ldctl_value.bv_val = NULL;
  1977. ctrl->ldctl_value.bv_len = 0;
  1978. }
  1979. if (zend_hash_find(Z_ARRVAL_PP(ctrlval), "iscritical", sizeof("iscritical"), (void **) &val) == SUCCESS) {
  1980. convert_to_boolean_ex(val);
  1981. ctrl->ldctl_iscritical = Z_BVAL_PP(val);
  1982. } else {
  1983. ctrl->ldctl_iscritical = 0;
  1984. }
  1985. ++ctrlp;
  1986. *ctrlp = NULL;
  1987. zend_hash_move_forward(Z_ARRVAL_PP(newval));
  1988. }
  1989. if (!error) {
  1990. error = ldap_set_option(ldap, option, ctrls);
  1991. }
  1992. ctrlp = ctrls;
  1993. while (*ctrlp) {
  1994. efree(*ctrlp);
  1995. ctrlp++;
  1996. }
  1997. efree(ctrls);
  1998. if (error) {
  1999. RETURN_FALSE;
  2000. }
  2001. } break;
  2002. default:
  2003. RETURN_FALSE;
  2004. }
  2005. RETURN_TRUE;
  2006. }
  2007. /* }}} */
  2008. #ifdef HAVE_LDAP_PARSE_RESULT
  2009. /* {{{ proto bool ldap_parse_result(resource link, resource result, int errcode, string matcheddn, string errmsg, array referrals)
  2010. Extract information from result */
  2011. PHP_FUNCTION(ldap_parse_result)
  2012. {
  2013. zval *link, *result, *errcode, *matcheddn, *errmsg, *referrals;
  2014. ldap_linkdata *ld;
  2015. LDAPMessage *ldap_result;
  2016. char **lreferrals, **refp;
  2017. char *lmatcheddn, *lerrmsg;
  2018. int rc, lerrcode, myargcount = ZEND_NUM_ARGS();
  2019. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrz|zzz", &link, &result, &errcode, &matcheddn, &errmsg, &referrals) != SUCCESS) {
  2020. return;
  2021. }
  2022. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  2023. ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
  2024. rc = ldap_parse_result(ld->link, ldap_result, &lerrcode,
  2025. myargcount > 3 ? &lmatcheddn : NULL,
  2026. myargcount > 4 ? &lerrmsg : NULL,
  2027. myargcount > 5 ? &lreferrals : NULL,
  2028. NULL /* &serverctrls */,
  2029. 0);
  2030. if (rc != LDAP_SUCCESS) {
  2031. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to parse result: %s", ldap_err2string(rc));
  2032. RETURN_FALSE;
  2033. }
  2034. zval_dtor(errcode);
  2035. ZVAL_LONG(errcode, lerrcode);
  2036. /* Reverse -> fall through */
  2037. switch (myargcount) {
  2038. case 6:
  2039. zval_dtor(referrals);
  2040. array_init(referrals);
  2041. if (lreferrals != NULL) {
  2042. refp = lreferrals;
  2043. while (*refp) {
  2044. add_next_index_string(referrals, *refp, 1);
  2045. refp++;
  2046. }
  2047. ldap_memvfree((void**)lreferrals);
  2048. }
  2049. case 5:
  2050. zval_dtor(errmsg);
  2051. if (lerrmsg == NULL) {
  2052. ZVAL_EMPTY_STRING(errmsg);
  2053. } else {
  2054. ZVAL_STRING(errmsg, lerrmsg, 1);
  2055. ldap_memfree(lerrmsg);
  2056. }
  2057. case 4:
  2058. zval_dtor(matcheddn);
  2059. if (lmatcheddn == NULL) {
  2060. ZVAL_EMPTY_STRING(matcheddn);
  2061. } else {
  2062. ZVAL_STRING(matcheddn, lmatcheddn, 1);
  2063. ldap_memfree(lmatcheddn);
  2064. }
  2065. }
  2066. RETURN_TRUE;
  2067. }
  2068. /* }}} */
  2069. #endif
  2070. /* {{{ proto resource ldap_first_reference(resource link, resource result)
  2071. Return first reference */
  2072. PHP_FUNCTION(ldap_first_reference)
  2073. {
  2074. zval *link, *result;
  2075. ldap_linkdata *ld;
  2076. ldap_resultentry *resultentry;
  2077. LDAPMessage *ldap_result, *entry;
  2078. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result) != SUCCESS) {
  2079. return;
  2080. }
  2081. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  2082. ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
  2083. if ((entry = ldap_first_reference(ld->link, ldap_result)) == NULL) {
  2084. RETVAL_FALSE;
  2085. } else {
  2086. resultentry = emalloc(sizeof(ldap_resultentry));
  2087. ZEND_REGISTER_RESOURCE(return_value, resultentry, le_result_entry);
  2088. resultentry->id = Z_LVAL_P(result);
  2089. zend_list_addref(resultentry->id);
  2090. resultentry->data = entry;
  2091. resultentry->ber = NULL;
  2092. }
  2093. }
  2094. /* }}} */
  2095. /* {{{ proto resource ldap_next_reference(resource link, resource reference_entry)
  2096. Get next reference */
  2097. PHP_FUNCTION(ldap_next_reference)
  2098. {
  2099. zval *link, *result_entry;
  2100. ldap_linkdata *ld;
  2101. ldap_resultentry *resultentry, *resultentry_next;
  2102. LDAPMessage *entry_next;
  2103. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &link, &result_entry) != SUCCESS) {
  2104. return;
  2105. }
  2106. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  2107. ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
  2108. if ((entry_next = ldap_next_reference(ld->link, resultentry->data)) == NULL) {
  2109. RETVAL_FALSE;
  2110. } else {
  2111. resultentry_next = emalloc(sizeof(ldap_resultentry));
  2112. ZEND_REGISTER_RESOURCE(return_value, resultentry_next, le_result_entry);
  2113. resultentry_next->id = resultentry->id;
  2114. zend_list_addref(resultentry->id);
  2115. resultentry_next->data = entry_next;
  2116. resultentry_next->ber = NULL;
  2117. }
  2118. }
  2119. /* }}} */
  2120. #ifdef HAVE_LDAP_PARSE_REFERENCE
  2121. /* {{{ proto bool ldap_parse_reference(resource link, resource reference_entry, array referrals)
  2122. Extract information from reference entry */
  2123. PHP_FUNCTION(ldap_parse_reference)
  2124. {
  2125. zval *link, *result_entry, *referrals;
  2126. ldap_linkdata *ld;
  2127. ldap_resultentry *resultentry;
  2128. char **lreferrals, **refp;
  2129. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrz", &link, &result_entry, &referrals) != SUCCESS) {
  2130. return;
  2131. }
  2132. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  2133. ZEND_FETCH_RESOURCE(resultentry, ldap_resultentry *, &result_entry, -1, "ldap result entry", le_result_entry);
  2134. if (ldap_parse_reference(ld->link, resultentry->data, &lreferrals, NULL /* &serverctrls */, 0) != LDAP_SUCCESS) {
  2135. RETURN_FALSE;
  2136. }
  2137. zval_dtor(referrals);
  2138. array_init(referrals);
  2139. if (lreferrals != NULL) {
  2140. refp = lreferrals;
  2141. while (*refp) {
  2142. add_next_index_string(referrals, *refp, 1);
  2143. refp++;
  2144. }
  2145. ldap_memvfree((void**)lreferrals);
  2146. }
  2147. RETURN_TRUE;
  2148. }
  2149. /* }}} */
  2150. #endif
  2151. /* {{{ proto bool ldap_rename(resource link, string dn, string newrdn, string newparent, bool deleteoldrdn);
  2152. Modify the name of an entry */
  2153. PHP_FUNCTION(ldap_rename)
  2154. {
  2155. zval *link;
  2156. ldap_linkdata *ld;
  2157. int rc;
  2158. char *dn, *newrdn, *newparent;
  2159. int dn_len, newrdn_len, newparent_len;
  2160. zend_bool deleteoldrdn;
  2161. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsssb", &link, &dn, &dn_len, &newrdn, &newrdn_len, &newparent, &newparent_len, &deleteoldrdn) != SUCCESS) {
  2162. return;
  2163. }
  2164. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  2165. if (newparent_len == 0) {
  2166. newparent = NULL;
  2167. }
  2168. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP
  2169. rc = ldap_rename_s(ld->link, dn, newrdn, newparent, deleteoldrdn, NULL, NULL);
  2170. #else
  2171. if (newparent_len != 0) {
  2172. php_error_docref(NULL TSRMLS_CC, E_WARNING, "You are using old LDAP API, newparent must be the empty string, can only modify RDN");
  2173. RETURN_FALSE;
  2174. }
  2175. /* could support old APIs but need check for ldap_modrdn2()/ldap_modrdn() */
  2176. rc = ldap_modrdn2_s(ld->link, dn, newrdn, deleteoldrdn);
  2177. #endif
  2178. if (rc == LDAP_SUCCESS) {
  2179. RETURN_TRUE;
  2180. }
  2181. RETURN_FALSE;
  2182. }
  2183. /* }}} */
  2184. #ifdef HAVE_LDAP_START_TLS_S
  2185. /* {{{ proto bool ldap_start_tls(resource link)
  2186. Start TLS */
  2187. PHP_FUNCTION(ldap_start_tls)
  2188. {
  2189. zval *link;
  2190. ldap_linkdata *ld;
  2191. int rc, protocol = LDAP_VERSION3;
  2192. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &link) != SUCCESS) {
  2193. return;
  2194. }
  2195. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  2196. if (((rc = ldap_set_option(ld->link, LDAP_OPT_PROTOCOL_VERSION, &protocol)) != LDAP_SUCCESS) ||
  2197. ((rc = ldap_start_tls_s(ld->link, NULL, NULL)) != LDAP_SUCCESS)
  2198. ) {
  2199. php_error_docref(NULL TSRMLS_CC, E_WARNING,"Unable to start TLS: %s", ldap_err2string(rc));
  2200. RETURN_FALSE;
  2201. } else {
  2202. RETURN_TRUE;
  2203. }
  2204. }
  2205. /* }}} */
  2206. #endif
  2207. #endif /* (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP */
  2208. #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
  2209. /* {{{ _ldap_rebind_proc()
  2210. */
  2211. int _ldap_rebind_proc(LDAP *ldap, const char *url, ber_tag_t req, ber_int_t msgid, void *params)
  2212. {
  2213. ldap_linkdata *ld;
  2214. int retval;
  2215. zval *cb_url;
  2216. zval **cb_args[2];
  2217. zval *cb_retval;
  2218. zval *cb_link = (zval *) params;
  2219. TSRMLS_FETCH();
  2220. ld = (ldap_linkdata *) zend_fetch_resource(&cb_link TSRMLS_CC, -1, "ldap link", NULL, 1, le_link);
  2221. /* link exists and callback set? */
  2222. if (ld == NULL || ld->rebindproc == NULL) {
  2223. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Link not found or no callback set");
  2224. return LDAP_OTHER;
  2225. }
  2226. /* callback */
  2227. MAKE_STD_ZVAL(cb_url);
  2228. ZVAL_STRING(cb_url, estrdup(url), 0);
  2229. cb_args[0] = &cb_link;
  2230. cb_args[1] = &cb_url;
  2231. if (call_user_function_ex(EG(function_table), NULL, ld->rebindproc, &cb_retval, 2, cb_args, 0, NULL TSRMLS_CC) == SUCCESS && cb_retval) {
  2232. convert_to_long_ex(&cb_retval);
  2233. retval = Z_LVAL_P(cb_retval);
  2234. zval_ptr_dtor(&cb_retval);
  2235. } else {
  2236. php_error_docref(NULL TSRMLS_CC, E_WARNING, "rebind_proc PHP callback failed");
  2237. retval = LDAP_OTHER;
  2238. }
  2239. zval_dtor(cb_url);
  2240. FREE_ZVAL(cb_url);
  2241. return retval;
  2242. }
  2243. /* }}} */
  2244. /* {{{ proto bool ldap_set_rebind_proc(resource link, string callback)
  2245. Set a callback function to do re-binds on referral chasing. */
  2246. PHP_FUNCTION(ldap_set_rebind_proc)
  2247. {
  2248. zval *link, *callback;
  2249. ldap_linkdata *ld;
  2250. char *callback_name;
  2251. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz", &link, &callback) != SUCCESS) {
  2252. RETURN_FALSE;
  2253. }
  2254. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  2255. if (Z_TYPE_P(callback) == IS_STRING && Z_STRLEN_P(callback) == 0) {
  2256. /* unregister rebind procedure */
  2257. if (ld->rebindproc != NULL) {
  2258. zval_dtor(ld->rebindproc);
  2259. FREE_ZVAL(ld->rebindproc);
  2260. ld->rebindproc = NULL;
  2261. ldap_set_rebind_proc(ld->link, NULL, NULL);
  2262. }
  2263. RETURN_TRUE;
  2264. }
  2265. /* callable? */
  2266. if (!zend_is_callable(callback, 0, &callback_name TSRMLS_CC)) {
  2267. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Two arguments expected for '%s' to be a valid callback", callback_name);
  2268. efree(callback_name);
  2269. RETURN_FALSE;
  2270. }
  2271. efree(callback_name);
  2272. /* register rebind procedure */
  2273. if (ld->rebindproc == NULL) {
  2274. ldap_set_rebind_proc(ld->link, _ldap_rebind_proc, (void *) link);
  2275. } else {
  2276. zval_dtor(ld->rebindproc);
  2277. }
  2278. ALLOC_ZVAL(ld->rebindproc);
  2279. *ld->rebindproc = *callback;
  2280. zval_copy_ctor(ld->rebindproc);
  2281. RETURN_TRUE;
  2282. }
  2283. /* }}} */
  2284. #endif
  2285. static void php_ldap_do_escape(const zend_bool *map, const char *value, size_t valuelen, char **result, size_t *resultlen)
  2286. {
  2287. char hex[] = "0123456789abcdef";
  2288. int i, p = 0;
  2289. size_t len = 0;
  2290. for (i = 0; i < valuelen; i++) {
  2291. len += (map[(unsigned char) value[i]]) ? 3 : 1;
  2292. }
  2293. (*result) = (char *) safe_emalloc_string(1, len, 1);
  2294. (*resultlen) = len;
  2295. for (i = 0; i < valuelen; i++) {
  2296. unsigned char v = (unsigned char) value[i];
  2297. if (map[v]) {
  2298. (*result)[p++] = '\\';
  2299. (*result)[p++] = hex[v >> 4];
  2300. (*result)[p++] = hex[v & 0x0f];
  2301. } else {
  2302. (*result)[p++] = v;
  2303. }
  2304. }
  2305. (*result)[p++] = '\0';
  2306. }
  2307. static void php_ldap_escape_map_set_chars(zend_bool *map, const char *chars, const int charslen, char escape)
  2308. {
  2309. int i = 0;
  2310. while (i < charslen) {
  2311. map[(unsigned char) chars[i++]] = escape;
  2312. }
  2313. }
  2314. PHP_FUNCTION(ldap_escape)
  2315. {
  2316. char *value, *ignores, *result;
  2317. int valuelen = 0, ignoreslen = 0, i;
  2318. size_t resultlen;
  2319. long flags = 0;
  2320. zend_bool map[256] = {0}, havecharlist = 0;
  2321. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sl", &value, &valuelen, &ignores, &ignoreslen, &flags) != SUCCESS) {
  2322. return;
  2323. }
  2324. if (!valuelen) {
  2325. RETURN_EMPTY_STRING();
  2326. }
  2327. if (flags & PHP_LDAP_ESCAPE_FILTER) {
  2328. havecharlist = 1;
  2329. php_ldap_escape_map_set_chars(map, "\\*()\0", sizeof("\\*()\0") - 1, 1);
  2330. }
  2331. if (flags & PHP_LDAP_ESCAPE_DN) {
  2332. havecharlist = 1;
  2333. php_ldap_escape_map_set_chars(map, "\\,=+<>;\"#", sizeof("\\,=+<>;\"#") - 1, 1);
  2334. }
  2335. if (!havecharlist) {
  2336. for (i = 0; i < 256; i++) {
  2337. map[i] = 1;
  2338. }
  2339. }
  2340. if (ignoreslen) {
  2341. php_ldap_escape_map_set_chars(map, ignores, ignoreslen, 0);
  2342. }
  2343. php_ldap_do_escape(map, value, valuelen, &result, &resultlen);
  2344. RETURN_STRINGL(result, resultlen, 0);
  2345. }
  2346. #ifdef STR_TRANSLATION
  2347. /* {{{ php_ldap_do_translate
  2348. */
  2349. static void php_ldap_do_translate(INTERNAL_FUNCTION_PARAMETERS, int way)
  2350. {
  2351. char *value;
  2352. int result, ldap_len;
  2353. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &value, &value_len) != SUCCESS) {
  2354. return;
  2355. }
  2356. if (value_len == 0) {
  2357. RETURN_FALSE;
  2358. }
  2359. if (way == 1) {
  2360. result = ldap_8859_to_t61(&value, &value_len, 0);
  2361. } else {
  2362. result = ldap_t61_to_8859(&value, &value_len, 0);
  2363. }
  2364. if (result == LDAP_SUCCESS) {
  2365. RETVAL_STRINGL(value, value_len, 1);
  2366. free(value);
  2367. } else {
  2368. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Conversion from iso-8859-1 to t61 failed: %s", ldap_err2string(result));
  2369. RETVAL_FALSE;
  2370. }
  2371. }
  2372. /* }}} */
  2373. /* {{{ proto string ldap_t61_to_8859(string value)
  2374. Translate t61 characters to 8859 characters */
  2375. PHP_FUNCTION(ldap_t61_to_8859)
  2376. {
  2377. php_ldap_do_translate(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2378. }
  2379. /* }}} */
  2380. /* {{{ proto string ldap_8859_to_t61(string value)
  2381. Translate 8859 characters to t61 characters */
  2382. PHP_FUNCTION(ldap_8859_to_t61)
  2383. {
  2384. php_ldap_do_translate(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2385. }
  2386. /* }}} */
  2387. #endif
  2388. #ifdef LDAP_CONTROL_PAGEDRESULTS
  2389. /* {{{ proto mixed ldap_control_paged_result(resource link, int pagesize [, bool iscritical [, string cookie]])
  2390. Inject paged results control*/
  2391. PHP_FUNCTION(ldap_control_paged_result)
  2392. {
  2393. long pagesize;
  2394. zend_bool iscritical;
  2395. zval *link;
  2396. char *cookie = NULL;
  2397. int cookie_len = 0;
  2398. struct berval lcookie = { 0, NULL };
  2399. ldap_linkdata *ld;
  2400. LDAP *ldap;
  2401. BerElement *ber = NULL;
  2402. LDAPControl ctrl, *ctrlsp[2];
  2403. int rc, myargcount = ZEND_NUM_ARGS();
  2404. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|bs", &link, &pagesize, &iscritical, &cookie, &cookie_len) != SUCCESS) {
  2405. return;
  2406. }
  2407. if (Z_TYPE_P(link) == IS_NULL) {
  2408. ldap = NULL;
  2409. } else {
  2410. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  2411. ldap = ld->link;
  2412. }
  2413. ber = ber_alloc_t(LBER_USE_DER);
  2414. if (ber == NULL) {
  2415. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to alloc BER encoding resources for paged results control");
  2416. RETURN_FALSE;
  2417. }
  2418. ctrl.ldctl_iscritical = 0;
  2419. switch (myargcount) {
  2420. case 4:
  2421. lcookie.bv_val = cookie;
  2422. lcookie.bv_len = cookie_len;
  2423. /* fallthru */
  2424. case 3:
  2425. ctrl.ldctl_iscritical = (int)iscritical;
  2426. /* fallthru */
  2427. }
  2428. if (ber_printf(ber, "{iO}", (int)pagesize, &lcookie) == LBER_ERROR) {
  2429. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to BER printf paged results control");
  2430. RETVAL_FALSE;
  2431. goto lcpr_error_out;
  2432. }
  2433. rc = ber_flatten2(ber, &ctrl.ldctl_value, 0);
  2434. if (rc == LBER_ERROR) {
  2435. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to BER encode paged results control");
  2436. RETVAL_FALSE;
  2437. goto lcpr_error_out;
  2438. }
  2439. ctrl.ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
  2440. if (ldap) {
  2441. /* directly set the option */
  2442. ctrlsp[0] = &ctrl;
  2443. ctrlsp[1] = NULL;
  2444. rc = ldap_set_option(ldap, LDAP_OPT_SERVER_CONTROLS, ctrlsp);
  2445. if (rc != LDAP_SUCCESS) {
  2446. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set paged results control: %s (%d)", ldap_err2string(rc), rc);
  2447. RETVAL_FALSE;
  2448. goto lcpr_error_out;
  2449. }
  2450. RETVAL_TRUE;
  2451. } else {
  2452. /* return a PHP control object */
  2453. array_init(return_value);
  2454. add_assoc_string(return_value, "oid", ctrl.ldctl_oid, 1);
  2455. if (ctrl.ldctl_value.bv_len) {
  2456. add_assoc_stringl(return_value, "value", ctrl.ldctl_value.bv_val, ctrl.ldctl_value.bv_len, 1);
  2457. }
  2458. if (ctrl.ldctl_iscritical) {
  2459. add_assoc_bool(return_value, "iscritical", ctrl.ldctl_iscritical);
  2460. }
  2461. }
  2462. lcpr_error_out:
  2463. if (ber != NULL) {
  2464. ber_free(ber, 1);
  2465. }
  2466. return;
  2467. }
  2468. /* }}} */
  2469. /* {{{ proto bool ldap_control_paged_result_response(resource link, resource result [, string &cookie [, int &estimated]])
  2470. Extract paged results control response */
  2471. PHP_FUNCTION(ldap_control_paged_result_response)
  2472. {
  2473. zval *link, *result, *cookie, *estimated;
  2474. struct berval lcookie;
  2475. int lestimated;
  2476. ldap_linkdata *ld;
  2477. LDAPMessage *ldap_result;
  2478. LDAPControl **lserverctrls, *lctrl;
  2479. BerElement *ber;
  2480. ber_tag_t tag;
  2481. int rc, lerrcode, myargcount = ZEND_NUM_ARGS();
  2482. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|zz", &link, &result, &cookie, &estimated) != SUCCESS) {
  2483. return;
  2484. }
  2485. ZEND_FETCH_RESOURCE(ld, ldap_linkdata *, &link, -1, "ldap link", le_link);
  2486. ZEND_FETCH_RESOURCE(ldap_result, LDAPMessage *, &result, -1, "ldap result", le_result);
  2487. rc = ldap_parse_result(ld->link,
  2488. ldap_result,
  2489. &lerrcode,
  2490. NULL, /* matcheddn */
  2491. NULL, /* errmsg */
  2492. NULL, /* referrals */
  2493. &lserverctrls,
  2494. 0);
  2495. if (rc != LDAP_SUCCESS) {
  2496. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to parse result: %s (%d)", ldap_err2string(rc), rc);
  2497. RETURN_FALSE;
  2498. }
  2499. if (lerrcode != LDAP_SUCCESS) {
  2500. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Result is: %s (%d)", ldap_err2string(lerrcode), lerrcode);
  2501. RETURN_FALSE;
  2502. }
  2503. if (lserverctrls == NULL) {
  2504. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No server controls in result");
  2505. RETURN_FALSE;
  2506. }
  2507. lctrl = ldap_control_find(LDAP_CONTROL_PAGEDRESULTS, lserverctrls, NULL);
  2508. if (lctrl == NULL) {
  2509. ldap_controls_free(lserverctrls);
  2510. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No paged results control response in result");
  2511. RETURN_FALSE;
  2512. }
  2513. ber = ber_init(&lctrl->ldctl_value);
  2514. if (ber == NULL) {
  2515. ldap_controls_free(lserverctrls);
  2516. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to alloc BER decoding resources for paged results control response");
  2517. RETURN_FALSE;
  2518. }
  2519. tag = ber_scanf(ber, "{io}", &lestimated, &lcookie);
  2520. (void)ber_free(ber, 1);
  2521. if (tag == LBER_ERROR) {
  2522. ldap_controls_free(lserverctrls);
  2523. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to decode paged results control response");
  2524. RETURN_FALSE;
  2525. }
  2526. if (lestimated < 0) {
  2527. ldap_controls_free(lserverctrls);
  2528. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid paged results control response value");
  2529. RETURN_FALSE;
  2530. }
  2531. ldap_controls_free(lserverctrls);
  2532. if (myargcount == 4) {
  2533. zval_dtor(estimated);
  2534. ZVAL_LONG(estimated, lestimated);
  2535. }
  2536. zval_dtor(cookie);
  2537. if (lcookie.bv_len == 0) {
  2538. ZVAL_EMPTY_STRING(cookie);
  2539. } else {
  2540. ZVAL_STRINGL(cookie, lcookie.bv_val, lcookie.bv_len, 1);
  2541. }
  2542. ldap_memfree(lcookie.bv_val);
  2543. RETURN_TRUE;
  2544. }
  2545. /* }}} */
  2546. #endif
  2547. /* {{{ arginfo */
  2548. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_connect, 0, 0, 0)
  2549. ZEND_ARG_INFO(0, hostname)
  2550. ZEND_ARG_INFO(0, port)
  2551. #ifdef HAVE_ORALDAP
  2552. ZEND_ARG_INFO(0, wallet)
  2553. ZEND_ARG_INFO(0, wallet_passwd)
  2554. ZEND_ARG_INFO(0, authmode)
  2555. #endif
  2556. ZEND_END_ARG_INFO()
  2557. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_resource, 0, 0, 1)
  2558. ZEND_ARG_INFO(0, link_identifier)
  2559. ZEND_END_ARG_INFO()
  2560. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_bind, 0, 0, 1)
  2561. ZEND_ARG_INFO(0, link_identifier)
  2562. ZEND_ARG_INFO(0, bind_rdn)
  2563. ZEND_ARG_INFO(0, bind_password)
  2564. ZEND_END_ARG_INFO()
  2565. #ifdef HAVE_LDAP_SASL
  2566. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_sasl_bind, 0, 0, 1)
  2567. ZEND_ARG_INFO(0, link)
  2568. ZEND_ARG_INFO(0, binddn)
  2569. ZEND_ARG_INFO(0, password)
  2570. ZEND_ARG_INFO(0, sasl_mech)
  2571. ZEND_ARG_INFO(0, sasl_realm)
  2572. ZEND_ARG_INFO(0, sasl_authz_id)
  2573. ZEND_ARG_INFO(0, props)
  2574. ZEND_END_ARG_INFO()
  2575. #endif
  2576. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_read, 0, 0, 3)
  2577. ZEND_ARG_INFO(0, link_identifier)
  2578. ZEND_ARG_INFO(0, base_dn)
  2579. ZEND_ARG_INFO(0, filter)
  2580. ZEND_ARG_INFO(0, attributes)
  2581. ZEND_ARG_INFO(0, attrsonly)
  2582. ZEND_ARG_INFO(0, sizelimit)
  2583. ZEND_ARG_INFO(0, timelimit)
  2584. ZEND_ARG_INFO(0, deref)
  2585. ZEND_END_ARG_INFO()
  2586. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_list, 0, 0, 3)
  2587. ZEND_ARG_INFO(0, link_identifier)
  2588. ZEND_ARG_INFO(0, base_dn)
  2589. ZEND_ARG_INFO(0, filter)
  2590. ZEND_ARG_INFO(0, attributes)
  2591. ZEND_ARG_INFO(0, attrsonly)
  2592. ZEND_ARG_INFO(0, sizelimit)
  2593. ZEND_ARG_INFO(0, timelimit)
  2594. ZEND_ARG_INFO(0, deref)
  2595. ZEND_END_ARG_INFO()
  2596. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_search, 0, 0, 3)
  2597. ZEND_ARG_INFO(0, link_identifier)
  2598. ZEND_ARG_INFO(0, base_dn)
  2599. ZEND_ARG_INFO(0, filter)
  2600. ZEND_ARG_INFO(0, attributes)
  2601. ZEND_ARG_INFO(0, attrsonly)
  2602. ZEND_ARG_INFO(0, sizelimit)
  2603. ZEND_ARG_INFO(0, timelimit)
  2604. ZEND_ARG_INFO(0, deref)
  2605. ZEND_END_ARG_INFO()
  2606. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_count_entries, 0, 0, 2)
  2607. ZEND_ARG_INFO(0, link_identifier)
  2608. ZEND_ARG_INFO(0, result_identifier)
  2609. ZEND_END_ARG_INFO()
  2610. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_first_entry, 0, 0, 2)
  2611. ZEND_ARG_INFO(0, link_identifier)
  2612. ZEND_ARG_INFO(0, result_identifier)
  2613. ZEND_END_ARG_INFO()
  2614. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_next_entry, 0, 0, 2)
  2615. ZEND_ARG_INFO(0, link_identifier)
  2616. ZEND_ARG_INFO(0, result_identifier)
  2617. ZEND_END_ARG_INFO()
  2618. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_entries, 0, 0, 2)
  2619. ZEND_ARG_INFO(0, link_identifier)
  2620. ZEND_ARG_INFO(0, result_identifier)
  2621. ZEND_END_ARG_INFO()
  2622. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_first_attribute, 0, 0, 2)
  2623. ZEND_ARG_INFO(0, link_identifier)
  2624. ZEND_ARG_INFO(0, result_entry_identifier)
  2625. ZEND_END_ARG_INFO()
  2626. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_next_attribute, 0, 0, 2)
  2627. ZEND_ARG_INFO(0, link_identifier)
  2628. ZEND_ARG_INFO(0, result_entry_identifier)
  2629. ZEND_END_ARG_INFO()
  2630. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_attributes, 0, 0, 2)
  2631. ZEND_ARG_INFO(0, link_identifier)
  2632. ZEND_ARG_INFO(0, result_entry_identifier)
  2633. ZEND_END_ARG_INFO()
  2634. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_values, 0, 0, 3)
  2635. ZEND_ARG_INFO(0, link_identifier)
  2636. ZEND_ARG_INFO(0, result_entry_identifier)
  2637. ZEND_ARG_INFO(0, attribute)
  2638. ZEND_END_ARG_INFO()
  2639. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_values_len, 0, 0, 3)
  2640. ZEND_ARG_INFO(0, link_identifier)
  2641. ZEND_ARG_INFO(0, result_entry_identifier)
  2642. ZEND_ARG_INFO(0, attribute)
  2643. ZEND_END_ARG_INFO()
  2644. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_dn, 0, 0, 2)
  2645. ZEND_ARG_INFO(0, link_identifier)
  2646. ZEND_ARG_INFO(0, result_entry_identifier)
  2647. ZEND_END_ARG_INFO()
  2648. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_explode_dn, 0, 0, 2)
  2649. ZEND_ARG_INFO(0, dn)
  2650. ZEND_ARG_INFO(0, with_attrib)
  2651. ZEND_END_ARG_INFO()
  2652. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_dn2ufn, 0, 0, 1)
  2653. ZEND_ARG_INFO(0, dn)
  2654. ZEND_END_ARG_INFO()
  2655. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_add, 0, 0, 3)
  2656. ZEND_ARG_INFO(0, link_identifier)
  2657. ZEND_ARG_INFO(0, dn)
  2658. ZEND_ARG_INFO(0, entry)
  2659. ZEND_END_ARG_INFO()
  2660. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_delete, 0, 0, 2)
  2661. ZEND_ARG_INFO(0, link_identifier)
  2662. ZEND_ARG_INFO(0, dn)
  2663. ZEND_END_ARG_INFO()
  2664. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_modify, 0, 0, 3)
  2665. ZEND_ARG_INFO(0, link_identifier)
  2666. ZEND_ARG_INFO(0, dn)
  2667. ZEND_ARG_INFO(0, entry)
  2668. ZEND_END_ARG_INFO()
  2669. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_modify_batch, 0, 0, 3)
  2670. ZEND_ARG_INFO(0, link_identifier)
  2671. ZEND_ARG_INFO(0, dn)
  2672. ZEND_ARG_ARRAY_INFO(0, modifications_info, 0)
  2673. ZEND_END_ARG_INFO()
  2674. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_mod_add, 0, 0, 3)
  2675. ZEND_ARG_INFO(0, link_identifier)
  2676. ZEND_ARG_INFO(0, dn)
  2677. ZEND_ARG_INFO(0, entry)
  2678. ZEND_END_ARG_INFO()
  2679. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_mod_replace, 0, 0, 3)
  2680. ZEND_ARG_INFO(0, link_identifier)
  2681. ZEND_ARG_INFO(0, dn)
  2682. ZEND_ARG_INFO(0, entry)
  2683. ZEND_END_ARG_INFO()
  2684. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_mod_del, 0, 0, 3)
  2685. ZEND_ARG_INFO(0, link_identifier)
  2686. ZEND_ARG_INFO(0, dn)
  2687. ZEND_ARG_INFO(0, entry)
  2688. ZEND_END_ARG_INFO()
  2689. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_err2str, 0, 0, 1)
  2690. ZEND_ARG_INFO(0, errno)
  2691. ZEND_END_ARG_INFO()
  2692. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_compare, 0, 0, 4)
  2693. ZEND_ARG_INFO(0, link_identifier)
  2694. ZEND_ARG_INFO(0, dn)
  2695. ZEND_ARG_INFO(0, attribute)
  2696. ZEND_ARG_INFO(0, value)
  2697. ZEND_END_ARG_INFO()
  2698. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_sort, 0, 0, 3)
  2699. ZEND_ARG_INFO(0, link)
  2700. ZEND_ARG_INFO(0, result)
  2701. ZEND_ARG_INFO(0, sortfilter)
  2702. ZEND_END_ARG_INFO()
  2703. #ifdef LDAP_CONTROL_PAGEDRESULTS
  2704. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_control_paged_result, 0, 0, 2)
  2705. ZEND_ARG_INFO(0, link)
  2706. ZEND_ARG_INFO(0, pagesize)
  2707. ZEND_ARG_INFO(0, iscritical)
  2708. ZEND_ARG_INFO(0, cookie)
  2709. ZEND_END_ARG_INFO();
  2710. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_control_paged_result_response, 0, 0, 2)
  2711. ZEND_ARG_INFO(0, link)
  2712. ZEND_ARG_INFO(0, result)
  2713. ZEND_ARG_INFO(1, cookie)
  2714. ZEND_ARG_INFO(1, estimated)
  2715. ZEND_END_ARG_INFO();
  2716. #endif
  2717. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP
  2718. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_rename, 0, 0, 5)
  2719. ZEND_ARG_INFO(0, link_identifier)
  2720. ZEND_ARG_INFO(0, dn)
  2721. ZEND_ARG_INFO(0, newrdn)
  2722. ZEND_ARG_INFO(0, newparent)
  2723. ZEND_ARG_INFO(0, deleteoldrdn)
  2724. ZEND_END_ARG_INFO()
  2725. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_get_option, 0, 0, 3)
  2726. ZEND_ARG_INFO(0, link_identifier)
  2727. ZEND_ARG_INFO(0, option)
  2728. ZEND_ARG_INFO(1, retval)
  2729. ZEND_END_ARG_INFO()
  2730. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_set_option, 0, 0, 3)
  2731. ZEND_ARG_INFO(0, link_identifier)
  2732. ZEND_ARG_INFO(0, option)
  2733. ZEND_ARG_INFO(0, newval)
  2734. ZEND_END_ARG_INFO()
  2735. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_first_reference, 0, 0, 2)
  2736. ZEND_ARG_INFO(0, link)
  2737. ZEND_ARG_INFO(0, result)
  2738. ZEND_END_ARG_INFO()
  2739. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_next_reference, 0, 0, 2)
  2740. ZEND_ARG_INFO(0, link)
  2741. ZEND_ARG_INFO(0, entry)
  2742. ZEND_END_ARG_INFO()
  2743. #ifdef HAVE_LDAP_PARSE_REFERENCE
  2744. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_parse_reference, 0, 0, 3)
  2745. ZEND_ARG_INFO(0, link)
  2746. ZEND_ARG_INFO(0, entry)
  2747. ZEND_ARG_INFO(1, referrals)
  2748. ZEND_END_ARG_INFO()
  2749. #endif
  2750. #ifdef HAVE_LDAP_PARSE_RESULT
  2751. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_parse_result, 0, 0, 3)
  2752. ZEND_ARG_INFO(0, link)
  2753. ZEND_ARG_INFO(0, result)
  2754. ZEND_ARG_INFO(1, errcode)
  2755. ZEND_ARG_INFO(1, matcheddn)
  2756. ZEND_ARG_INFO(1, errmsg)
  2757. ZEND_ARG_INFO(1, referrals)
  2758. ZEND_END_ARG_INFO()
  2759. #endif
  2760. #endif
  2761. #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
  2762. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_set_rebind_proc, 0, 0, 2)
  2763. ZEND_ARG_INFO(0, link)
  2764. ZEND_ARG_INFO(0, callback)
  2765. ZEND_END_ARG_INFO()
  2766. #endif
  2767. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_escape, 0, 0, 1)
  2768. ZEND_ARG_INFO(0, value)
  2769. ZEND_ARG_INFO(0, ignore)
  2770. ZEND_ARG_INFO(0, flags)
  2771. ZEND_END_ARG_INFO()
  2772. #ifdef STR_TRANSLATION
  2773. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_t61_to_8859, 0, 0, 1)
  2774. ZEND_ARG_INFO(0, value)
  2775. ZEND_END_ARG_INFO()
  2776. ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_8859_to_t61, 0, 0, 1)
  2777. ZEND_ARG_INFO(0, value)
  2778. ZEND_END_ARG_INFO()
  2779. #endif
  2780. /* }}} */
  2781. /*
  2782. This is just a small subset of the functionality provided by the LDAP library. All the
  2783. operations are synchronous. Referrals are not handled automatically.
  2784. */
  2785. /* {{{ ldap_functions[]
  2786. */
  2787. const zend_function_entry ldap_functions[] = {
  2788. PHP_FE(ldap_connect, arginfo_ldap_connect)
  2789. PHP_FALIAS(ldap_close, ldap_unbind, arginfo_ldap_resource)
  2790. PHP_FE(ldap_bind, arginfo_ldap_bind)
  2791. #ifdef HAVE_LDAP_SASL
  2792. PHP_FE(ldap_sasl_bind, arginfo_ldap_sasl_bind)
  2793. #endif
  2794. PHP_FE(ldap_unbind, arginfo_ldap_resource)
  2795. PHP_FE(ldap_read, arginfo_ldap_read)
  2796. PHP_FE(ldap_list, arginfo_ldap_list)
  2797. PHP_FE(ldap_search, arginfo_ldap_search)
  2798. PHP_FE(ldap_free_result, arginfo_ldap_resource)
  2799. PHP_FE(ldap_count_entries, arginfo_ldap_count_entries)
  2800. PHP_FE(ldap_first_entry, arginfo_ldap_first_entry)
  2801. PHP_FE(ldap_next_entry, arginfo_ldap_next_entry)
  2802. PHP_FE(ldap_get_entries, arginfo_ldap_get_entries)
  2803. PHP_FE(ldap_first_attribute, arginfo_ldap_first_attribute)
  2804. PHP_FE(ldap_next_attribute, arginfo_ldap_next_attribute)
  2805. PHP_FE(ldap_get_attributes, arginfo_ldap_get_attributes)
  2806. PHP_FALIAS(ldap_get_values, ldap_get_values_len, arginfo_ldap_get_values)
  2807. PHP_FE(ldap_get_values_len, arginfo_ldap_get_values_len)
  2808. PHP_FE(ldap_get_dn, arginfo_ldap_get_dn)
  2809. PHP_FE(ldap_explode_dn, arginfo_ldap_explode_dn)
  2810. PHP_FE(ldap_dn2ufn, arginfo_ldap_dn2ufn)
  2811. PHP_FE(ldap_add, arginfo_ldap_add)
  2812. PHP_FE(ldap_delete, arginfo_ldap_delete)
  2813. PHP_FE(ldap_modify_batch, arginfo_ldap_modify_batch)
  2814. PHP_FALIAS(ldap_modify, ldap_mod_replace, arginfo_ldap_modify)
  2815. /* additional functions for attribute based modifications, Gerrit Thomson */
  2816. PHP_FE(ldap_mod_add, arginfo_ldap_mod_add)
  2817. PHP_FE(ldap_mod_replace, arginfo_ldap_mod_replace)
  2818. PHP_FE(ldap_mod_del, arginfo_ldap_mod_del)
  2819. /* end gjt mod */
  2820. PHP_FE(ldap_errno, arginfo_ldap_resource)
  2821. PHP_FE(ldap_err2str, arginfo_ldap_err2str)
  2822. PHP_FE(ldap_error, arginfo_ldap_resource)
  2823. PHP_FE(ldap_compare, arginfo_ldap_compare)
  2824. PHP_FE(ldap_sort, arginfo_ldap_sort)
  2825. #if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP
  2826. PHP_FE(ldap_rename, arginfo_ldap_rename)
  2827. PHP_FE(ldap_get_option, arginfo_ldap_get_option)
  2828. PHP_FE(ldap_set_option, arginfo_ldap_set_option)
  2829. PHP_FE(ldap_first_reference, arginfo_ldap_first_reference)
  2830. PHP_FE(ldap_next_reference, arginfo_ldap_next_reference)
  2831. #ifdef HAVE_LDAP_PARSE_REFERENCE
  2832. PHP_FE(ldap_parse_reference, arginfo_ldap_parse_reference)
  2833. #endif
  2834. #ifdef HAVE_LDAP_PARSE_RESULT
  2835. PHP_FE(ldap_parse_result, arginfo_ldap_parse_result)
  2836. #endif
  2837. #ifdef HAVE_LDAP_START_TLS_S
  2838. PHP_FE(ldap_start_tls, arginfo_ldap_resource)
  2839. #endif
  2840. #endif
  2841. #if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC)
  2842. PHP_FE(ldap_set_rebind_proc, arginfo_ldap_set_rebind_proc)
  2843. #endif
  2844. PHP_FE(ldap_escape, arginfo_ldap_escape)
  2845. #ifdef STR_TRANSLATION
  2846. PHP_FE(ldap_t61_to_8859, arginfo_ldap_t61_to_8859)
  2847. PHP_FE(ldap_8859_to_t61, arginfo_ldap_8859_to_t61)
  2848. #endif
  2849. #ifdef LDAP_CONTROL_PAGEDRESULTS
  2850. PHP_FE(ldap_control_paged_result, arginfo_ldap_control_paged_result)
  2851. PHP_FE(ldap_control_paged_result_response, arginfo_ldap_control_paged_result_response)
  2852. #endif
  2853. PHP_FE_END
  2854. };
  2855. /* }}} */
  2856. zend_module_entry ldap_module_entry = { /* {{{ */
  2857. STANDARD_MODULE_HEADER,
  2858. "ldap",
  2859. ldap_functions,
  2860. PHP_MINIT(ldap),
  2861. PHP_MSHUTDOWN(ldap),
  2862. NULL,
  2863. NULL,
  2864. PHP_MINFO(ldap),
  2865. NO_VERSION_YET,
  2866. PHP_MODULE_GLOBALS(ldap),
  2867. PHP_GINIT(ldap),
  2868. NULL,
  2869. NULL,
  2870. STANDARD_MODULE_PROPERTIES_EX
  2871. };
  2872. /* }}} */
  2873. /*
  2874. * Local variables:
  2875. * tab-width: 4
  2876. * c-basic-offset: 4
  2877. * End:
  2878. * vim600: sw=4 ts=4 fdm=marker
  2879. * vim<600: sw=4 ts=4
  2880. */